<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController
{

	/**
	 * 检测是否登录
	 * */
	public function _initialize()
	{
		if (empty(session('user_id'))) {
			header('Location: ' . U('/Home/Login/Login'));
			exit;
		} else {
			//再来判断权限
			if (session('Supertube') == 1) {
				//超管 拥有所有权限
			} else {
				$id = session('user_id');//不是超管的时候 user_id是account_number的主键ID
				$jurisdiction = M('account_number')->where("id=$id")->getField('jurisdiction');
				$action_name = ACTION_NAME;
				if ($action_name == index || $action_name == Index) {
					//访问数据统计
					$domain = strstr($jurisdiction, ',1,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == rankingList || $action_name == RankingList) {
					//排行榜
					$domain = strstr($jurisdiction, ',2,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == clueList || $action_name == ClueList) {
					//线索列表
					$domain = strstr($jurisdiction, ',3,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == customer || $action_name == Customer) {
					//客户列表
					$domain = strstr($jurisdiction, ',4,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				} else if ($action_name == follow_up_log || $action_name == Follow_up_log) {
					//跟进记录
					$domain = strstr($jurisdiction, ',5,');
					if (!$domain) {
						$this->error('没有访问权限');
					}
				}
			}
		}
	}

	//系统首页
	public function index()
	{
		$iid = session('iid');
		$uid = session('user_id');
		$member = D('ucenter_member')->where(array('id' => $uid))->find();

		$search = $_REQUEST['search'] ? $_REQUEST['search'] : 1;

		//线索
		$clue_map['iid'] = array('eq', $iid);
		$clue_where['uid'] = array('eq', $uid);
		$clue_where['distributor_id'] = array('eq', $uid);
		$clue_where['_logic'] = 'or';
		$clue_map['_complex'] = $clue_where;
		$clue_count = D('clue')->where($clue_map)->count();
		$this->assign('clue_count', $clue_count);

		//已转
		$clue_map['source'] = array('eq', 6);
		$transferred = D('clue')->where($clue_map)->count();
		$this->assign('transferred', $transferred);

		//转客户率
		$customerTransferRate = intval($transferred / $clue_count * 100);
		$this->assign('customerTransferRate', $customerTransferRate);

		//客户总数
		$customer_map['iid'] = array('eq', $iid);
		$customer_map['uid'] = array('eq', $uid);
		$customer_count = D('customer')->where($customer_map)->count();
		$this->assign('customer_count', $customer_count);

		//成交客户
		$deal_customer_map['iid'] = array('eq', $iid);
		$deal_customer_map['uid'] = array('eq', $uid);
		$deal_customer_map['type'] = array('eq', 1);
		$deal_customer_count = D('follow_up')->where($deal_customer_map)->group('cid')->count();
		$deal_customer_count = $deal_customer_count ? $deal_customer_count : 0;
		$this->assign('deal_customer_count', $deal_customer_count);

		//成交率
		$dealRate = intval($deal_customer_count / $customer_count * 100);
		$this->assign('dealRate', $dealRate);

		//介绍客户
		$introduce_map['iid'] = array('eq', $iid);
		$introduce_map['uid'] = array('eq', $uid);
		$introduce_map['source'] = array('eq', 5);
		$introduce_customer_count = D('customer')->where($introduce_map)->count();
		$this->assign('introduce_customer_count', $introduce_customer_count);

		//介绍率
		$introduceRate = intval($introduce_customer_count / $customer_count * 100);
		$this->assign('introduceRate', $introduceRate);

		//营业额
		$order_map['iid'] = array('eq', $iid);
		// $order_map['pa_id'] = array('eq',$uid);
		$order_map['is_pay'] = array('eq', 1);
		$order_map['type'] = array(array('eq', 1), array('eq', 2), array('eq', 3), 'OR');
		$turnover = D('order')->where($order_map)->sum('pay_price');
		// print_r($turnover);die;
		// $turnover = sprintf("%.1f",$turnover / 10000);
		$turnover = sprintf($turnover / 10000);
		$this->assign('turnover', $turnover);

		//今日营业额
		$day_order_map['iid'] = array('eq', $iid);
		// $day_order_map['pa_id'] = array('eq',$uid);
		$day_order_map['is_pay'] = array('eq', 1);
		$day_order_map['type'] = array(array('eq', 1), array('eq', 2), array('eq', 3), 'OR');
		$day_order_map['pay_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', time()))), array('elt', strtotime(date('Y-m-d 23:59:59', time()))), 'AND');
		$dayTurnover = D('order')->where($day_order_map)->sum('pay_price');
		$dayTurnover = sprintf($dayTurnover / 10000);
		$this->assign('dayTurnover', $dayTurnover);

		//营业额-较昨日增长
		$yesterday_order_map['iid'] = array('eq', $iid);
		// $yesterday_order_map['pa_id'] = array('eq',$uid);
		$yesterday_order_map['is_pay'] = array('eq', 1);
		$yesterday_order_map['type'] = array(array('eq', 1), array('eq', 2), array('eq', 3), 'OR');
		$yesterday_order_map['pay_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', strtotime("-1 day")))), array('elt', strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")))), 'AND');
		$yesterdayTurnover = D('order')->where($yesterday_order_map)->sum('pay_price');
		$yesterdayTurnover = sprintf("%.1f", $yesterdayTurnover / 10000);
		$dayTurnover = intval($dayTurnover / 100);
		$yesterdayTurnover = intval($yesterdayTurnover / 100);
		$yye_increase = $dayTurnover - $yesterdayTurnover;
		if ($yye_increase <= 0) {
			$yye_increase = 0;
		}
		$this->assign('yye_increase', $yye_increase);

		//跟进
		$follow_map['iid'] = array('eq', $iid);
		$follow_map['uid'] = array('eq', $uid);
		$follow_count = D('follow_up')->where($follow_map)->count();
		$this->assign('follow_count', $follow_count);

		//今日跟进
		$day_follow_map['iid'] = array('eq', $iid);
		$day_follow_map['uid'] = array('eq', $uid);
		$day_follow_map['create_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', time()))), array('elt', strtotime(date('Y-m-d 23:59:59', time()))), 'AND');
		$day_follow_count = D('follow_up')->where($day_follow_map)->count();
		$this->assign('day_follow_count', $day_follow_count);

		//跟进-较昨日增长
		$yesterday_follow_map['iid'] = array('eq', $iid);
		$yesterday_follow_map['uid'] = array('eq', $uid);
		$yesterday_follow_map['create_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', strtotime("-1 day")))), array('elt', strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")))), 'AND');
		$yesterday_follow_count = D('follow_up')->where($yesterday_follow_map)->count();
		$day_follow_count = intval($day_follow_count / 100);
		$yesterday_follow_count = intval($yesterday_follow_count / 100);
		$gj_increase = $day_follow_count - $yesterday_follow_count;
		if ($gj_increase <= 0) {
			$gj_increase = 0;
		}
		$this->assign('gj_increase', $gj_increase);

		$contactNumberList = D('follow_up_log')->field('uid,SUM(customer) as customer,SUM(clue) as clue')->where(array('iid' => $iid))->group('uid')->order("SUM(customer)+SUM(clue) desc")->limit(5)->select();
		foreach ($contactNumberList as $key => $item) {
			//线索数量
			$clue_map2['iid'] = array('eq', $iid);
			$clue_where2['uid'] = array('eq', $item['uid']);
			$clue_where2['distributor_id'] = array('eq', $item['uid']);
			$clue_where2['_logic'] = 'or';
			$clue_map2['_complex'] = $clue_where2;
			$clue_count = D('clue')->where($clue_map2)->count();
			$contactNumberList[$key]['clue_count'] = $clue_count ? $clue_count : 0;

			//客户数量
			$customer_map2['iid'] = array('eq', $iid);
			$customer_map2['uid'] = array('eq', $item['uid']);
			$customer_count = D('customer')->where($customer_map2)->count();
			$contactNumberList[$key]['customer_count'] = $customer_count ? $customer_count : 0;

			//线索与客户联系数总和
			$contactNumberList[$key]['total_contact_number'] = $item['customer'] + $item['clue'];

			//线索与客户总和
			$contactNumberList[$key]['total_count'] = $contactNumberList[$key]['clue_count'] + $contactNumberList[$key]['customer_count'];

			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$contactNumberList[$key]['username'] = $username ? $username : '';
		}

		$this->assign('contactNumberList', $contactNumberList);

		$order_map2['iid'] = array('eq', $iid);
		$order_map2['is_pay'] = array('eq', 1);
		// $yye_list = D('order')->field('pa_id,SUM(pay_price) as pay_price')->where($order_map2)->group("pa_id")->order('SUM(pay_price) desc')->limit(5)->select();
		// foreach ($yye_list as $key=>$item) {
		//     $username = D('ucenter_member')->where(array('id'=>$item['pa_id']))->getField('username');
		//     $yye_list[$key]['username'] = $username?$username:'';
		// }

		//tzb
		$yye_list = D('order')->field('uid,SUM(pay_price) as pay_price')->where($order_map2)->group("uid")->order('SUM(pay_price) desc')->limit(50)->select();
		// echo '<pre>';print_r($yye_list);die;
		foreach ($yye_list as $key => $item) {
			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$yye_list[$key]['username'] = $username ? $username : '';
		}
		$this->assign('yye_list', $yye_list);

		$signUp_map['iid'] = array('eq', $iid);
		$signUp_map['stage'] = array('eq', 2);
		$signUpList = D('follow_up')->field('uid,count(*) as `count`')->where($signUp_map)->group('uid')->order('`count` desc')->limit(5)->select();
		foreach ($signUpList as $key => $item) {
			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$signUpList[$key]['username'] = $username ? $username : '';
		}
		$this->assign('signUpList', $signUpList);

		$new_map['iid'] = array('eq', $iid);
		$new_map['stage'] = array('eq', 2);
		$news = D('follow_up')->where($new_map)->order('create_time desc')->limit(5)->select();
		foreach ($news as $key => $item) {
			$name = D('customer')->where(array('id' => $item['cid']))->getField('name');
			$news[$key]['name'] = $name ? $name : '';

			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$news[$key]['username'] = $username ? $username : '';
		}
		$this->assign('news', $news);

		$this->assign('member', $member);

		if ($search == 1) {
			$line_title = "去年数据,去年个人数据,今年数据,今年个人数据";
			$this->assign('line_title', $line_title);

			$line_time = date('m-d', time());
			$this->assign('line_time', $line_time);
			//今年数据
			$thisYear_start_time = strtotime(date('Y-m-d 00:00:00', time()));
			$thisYear_end_time = strtotime(date('Y-m-d 23:59:59', time()));
			$statistics_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= {$thisYear_start_time} and create_time <= {$thisYear_end_time}";
			$thisYearAllData = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('thisYearAllData', $thisYearAllData[0]['count']);

			//去年数据
			$start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-1 year")));
			$end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-1 year")));
			$allData_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time}";
			$allData = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$allData_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('allData', $allData[0]['count']);

			//今年个人数据
			$statistics_where .= " and uid = " . $uid;
			$year_personal_data = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('year_personal_data', $year_personal_data[0]['count']);

			//去年个人数据
			$allData_where .= " and uid = " . $uid;
			$personal_data = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$allData_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('personal_data', $personal_data[0]['count']);
		} else if ($search == 2) {
			$line_title = "去年数据,去年个人数据,今年数据,今年个人数据";
			$this->assign('line_title', $line_title);

			$line_time = date('m-d', strtotime("-1 day"));
			$this->assign('line_time', $line_time);
			//今年数据
			$thisYear_start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-1 day")));
			$thisYear_end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")));
			$statistics_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= {$thisYear_start_time} and create_time <= {$thisYear_end_time}";
			$thisYearAllData = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('thisYearAllData', $thisYearAllData[0]['count']);

			//去年数据
			$start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 24 * 60 * 60));
			$end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 24 * 60 * 60));
			$allData_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time}";
			$allData = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$allData_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('allData', $allData[0]['count']);

			//今年个人数据
			$statistics_where .= " and uid = " . $uid;
			$year_personal_data = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('year_personal_data', $year_personal_data[0]['count']);

			//去年个人数据
			$allData_where .= " and uid = " . $uid;
			$personal_data = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$allData_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");
			$this->assign('personal_data', $personal_data[0]['count']);
		} else if ($search == 3) {
			$line_title = "去年数据,去年个人数据,今年数据,今年个人数据";
			$this->assign('line_title', $line_title);

			$line_time = date('m-d', strtotime("-6 day"));
			$line_time .= "," . date('m-d', strtotime("-5 day"));
			$line_time .= "," . date('m-d', strtotime("-4 day"));
			$line_time .= "," . date('m-d', strtotime("-3 day"));
			$line_time .= "," . date('m-d', strtotime("-2 day"));
			$line_time .= "," . date('m-d', strtotime("-1 day"));
			$line_time .= "," . date('m-d', time());
			$this->assign('line_time', $line_time);

			//今年数据
			$statistics_six_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-6 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-6 day")));
			$thisYearAllData_six = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_six_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_five_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-5 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-5 day")));
			$thisYearAllData_five = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_five_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_four_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-4 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-4 day")));
			$thisYearAllData_four = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_four_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_three_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-3 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-3 day")));
			$thisYearAllData_three = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_three_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_two_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-2 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-2 day")));
			$thisYearAllData_two = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_two_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_one_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")));
			$thisYearAllData_one = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_one_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_zero_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', time())) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', time()));
			$thisYearAllData_zero = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_zero_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$thisYearAllData_six_val = $thisYearAllData_six[0]['count'] ? $thisYearAllData_six[0]['count'] : "0";
			$thisYearAllData_five_val = $thisYearAllData_five[0]['count'] ? $thisYearAllData_five[0]['count'] : "0";
			$thisYearAllData_four_val = $thisYearAllData_four[0]['count'] ? $thisYearAllData_four[0]['count'] : "0";
			$thisYearAllData_three_val = $thisYearAllData_three[0]['count'] ? $thisYearAllData_three[0]['count'] : "0";
			$thisYearAllData_two_val = $thisYearAllData_two[0]['count'] ? $thisYearAllData_two[0]['count'] : "0";
			$thisYearAllData_one_val = $thisYearAllData_one[0]['count'] ? $thisYearAllData_one[0]['count'] : "0";
			$thisYearAllData_zero_val = $thisYearAllData_zero[0]['count'] ? $thisYearAllData_zero[0]['count'] : "0";
			$thisYearAllData = $thisYearAllData_six_val . "," . $thisYearAllData_five_val . "," . $thisYearAllData_four_val . "," . $thisYearAllData_three_val . "," . $thisYearAllData_two_val . "," . $thisYearAllData_one_val . "," . $thisYearAllData_zero_val;
			$this->assign('thisYearAllData', $thisYearAllData);

			//今年个人数据
			$statistics_personal_six_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-6 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-6 day")));
			$thisYearAllData_personal_six = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_six_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_five_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-5 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-5 day")));
			$thisYearAllData_personal_five = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_five_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_four_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-4 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-4 day")));
			$thisYearAllData_personal_four = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_four_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_three_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-3 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-3 day")));
			$thisYearAllData_personal_three = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_three_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_two_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-2 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-2 day")));
			$thisYearAllData_personal_two = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_two_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_one_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 day"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")));
			$thisYearAllData_personal_one = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_one_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$statistics_personal_zero_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', time())) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', time()));
			$thisYearAllData_personal_zero = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$statistics_personal_zero_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$thisYearAllData_personal_six_val = $thisYearAllData_personal_six[0]['count'] ? $thisYearAllData_personal_six[0]['count'] : "0";
			$thisYearAllData_personal_five_val = $thisYearAllData_personal_five[0]['count'] ? $thisYearAllData_personal_five[0]['count'] : "0";
			$thisYearAllData_personal_four_val = $thisYearAllData_personal_four[0]['count'] ? $thisYearAllData_personal_four[0]['count'] : "0";
			$thisYearAllData_personal_three_val = $thisYearAllData_personal_three[0]['count'] ? $thisYearAllData_personal_three[0]['count'] : "0";
			$thisYearAllData_personal_two_val = $thisYearAllData_personal_two[0]['count'] ? $thisYearAllData_personal_two[0]['count'] : "0";
			$thisYearAllData_personal_one_val = $thisYearAllData_personal_one[0]['count'] ? $thisYearAllData_personal_one[0]['count'] : "0";
			$thisYearAllData_personal_zero_val = $thisYearAllData_personal_zero[0]['count'] ? $thisYearAllData_personal_zero[0]['count'] : "0";
			$year_personal_data = $thisYearAllData_personal_six_val . "," . $thisYearAllData_personal_five_val . "," . $thisYearAllData_personal_four_val . "," . $thisYearAllData_personal_three_val . "," . $thisYearAllData_personal_two_val . "," . $thisYearAllData_personal_one_val . "," . $thisYearAllData_personal_zero_val;
			$this->assign('year_personal_data', $year_personal_data);

			//去年数据
			$all_six_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 6 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 6 * 24 * 60 * 60));
			$allData_six = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_six_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_five_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 5 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 5 * 24 * 60 * 60));
			$allData_five = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_five_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_four_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 4 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 4 * 24 * 60 * 60));
			$allData_four = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_four_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_three_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 3 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 3 * 24 * 60 * 60));
			$allData_three = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_three_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_two_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 2 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 2 * 24 * 60 * 60));
			$allData_two = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_two_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_one_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 1 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 1 * 24 * 60 * 60));
			$allData_one = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_one_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$all_zero_where = "stage = 2 and `type` = 1 and iid = {$iid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year")));
			$allData_zero = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$all_zero_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$allData_six_val = $allData_six[0]['count'] ? $allData_six[0]['count'] : "0";
			$allData_five_val = $allData_five[0]['count'] ? $allData_five[0]['count'] : "0";
			$allData_four_val = $allData_four[0]['count'] ? $allData_four[0]['count'] : "0";
			$allData_three_val = $allData_three[0]['count'] ? $allData_three[0]['count'] : "0";
			$allData_two_val = $allData_two[0]['count'] ? $allData_two[0]['count'] : "0";
			$allData_one_val = $allData_one[0]['count'] ? $allData_one[0]['count'] : "0";
			$allData_zero_val = $allData_zero[0]['count'] ? $allData_zero[0]['count'] : "0";
			$allData = $allData_six_val . "," . $allData_five_val . "," . $allData_four_val . "," . $allData_three_val . "," . $allData_two_val . "," . $allData_one_val . "," . $allData_zero_val;
			$this->assign('allData', $allData);

			//去年个人数据
			$personal_six_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 6 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 6 * 24 * 60 * 60));
			$personalData_six = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_six_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_five_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 5 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 5 * 24 * 60 * 60));
			$personalData_five = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_five_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_four_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 4 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 4 * 24 * 60 * 60));
			$personalData_four = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_four_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_three_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 3 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 3 * 24 * 60 * 60));
			$personalData_three = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_three_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_two_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 2 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 2 * 24 * 60 * 60));
			$personalData_two = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_two_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_one_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year") - 1 * 24 * 60 * 60)) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year") - 1 * 24 * 60 * 60));
			$personalData_one = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_one_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personal_zero_where = "stage = 2 and `type` = 1 and iid = {$iid} and uid = {$uid} and create_time >= " . strtotime(date('Y-m-d 00:00:00', strtotime("-1 year"))) . " and create_time <= " . strtotime(date('Y-m-d 23:59:59', strtotime("-1 year")));
			$personalData_zero = D()->query("select SUM(a.count) as `count` from (select count(*) as count from teach_follow_up where {$personal_zero_where} group by FROM_UNIXTIME(create_time,'%Y%m%d'))a");

			$personalData_six_val = $personalData_six[0]['count'] ? $personalData_six[0]['count'] : "0";
			$personalData_five_val = $personalData_five[0]['count'] ? $personalData_five[0]['count'] : "0";
			$personalData_four_val = $personalData_four[0]['count'] ? $personalData_four[0]['count'] : "0";
			$personalData_three_val = $personalData_three[0]['count'] ? $personalData_three[0]['count'] : "0";
			$personalData_two_val = $personalData_two[0]['count'] ? $personalData_two[0]['count'] : "0";
			$personalData_one_val = $personalData_one[0]['count'] ? $personalData_one[0]['count'] : "0";
			$personalData_zero_val = $personalData_zero[0]['count'] ? $personalData_zero[0]['count'] : "0";
			$personal_data = $personalData_six_val . "," . $personalData_five_val . "," . $personalData_four_val . "," . $personalData_three_val . "," . $personalData_two_val . "," . $personalData_one_val . "," . $personalData_zero_val;
			$this->assign('personal_data', $personal_data);
		} else {
			$line_title = "总数据";
			$this->assign('line_title', $line_title);

			$where = "stage = 2 and `type` = 1 and iid = {$iid}";
			$start_time = strtotime($_REQUEST['start_time']);
			$end_time = strtotime($_REQUEST['end_time'] . " 23:59:59");
			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$where .= " and create_time >= {$start_time} and create_time <= {$end_time}";
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$where .= " and create_time >= {$start_time}";
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$where .= " and create_time <= {$end_time}";
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			$line_list = D()->query("select count(*) as `count`,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up where {$where} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by create_time asc");
			$line_time = "";
			$allData = "";
			foreach ($line_list as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$allData .= $item['count'] . ",";
			}
			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$allData = mb_substr($allData, 0, mb_strlen($allData, 'UTF-8') - 1, 'UTF-8');
			$this->assign('allData', $allData);
		}

		$this->assign('search', $search);
		$this->assign('cur', '数据统计');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 更新机构
	 */
	public function updateInstitution()
	{
		if (IS_POST) {
			$uid = session('user_id');
			$data['name'] = $_REQUEST['name'];
			$data['username'] = $_REQUEST['username'];
			$data['mobile'] = $_REQUEST['mobile'];
			$data['IDcard'] = $_REQUEST['IDcard'];
			$data['card_front'] = $_REQUEST['card_front'];
			$data['card_behind'] = $_REQUEST['card_behind'];
			$data['business_license'] = $_REQUEST['business_license'];
			$time = time();
			$data['update_time'] = $time;
			$data['status'] = 0;

			$institution = D('institution')->where(array('uid' => $uid))->find();
			if (count($institution) > 0) {
				if ($institution['status'] == 1) {
					$this->error("抱歉，审核通过后，不能修改信息");
				}
				$result = D('institution')->where(array('uid' => $uid))->save($data);
			} else {
				$data['create_time'] = $time;
				$data['uid'] = $uid;
				$result = D('institution')->add($data);
			}
			if ($result) {
				$this->success("更新成功");
			} else {
				$this->error("更新失败");
			}
		} else {
			$userInfo = D('ucenter_member a')->field('a.username,a.mobile,b.*')->join(" left join teach_institution b on a.id = b.uid")->where(array('a.id' => session('user_id'), 'a.user_type' => 3))->find();
			$this->assign('userInfo', $userInfo);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 更新基本信息
	 */
	public function updateBasicInfo()
	{
		if (IS_POST) {
			$uid = session('user_id');
			$data['name'] = $_REQUEST['name'];
			$data['logo'] = $_REQUEST['logo'];
			$data['banner'] = $_REQUEST['banner'];
			$data['desc'] = $_REQUEST['desc'];

			$institution = D('institution')->where(array('uid' => $uid))->find();
			if (count($institution) <= 0) {
				$this->error("请先认证");
			}

			if ($institution['status'] == 1) {
				$this->error("抱歉，审核通过后，不能修改信息");
			}

			$data['status'] = 0;
			$data['update_time'] = time();
			$result = D('institution')->where(array('uid' => $uid))->save($data);
			if ($result) {
				$this->success("保存成功");
			} else {
				$this->error("保存失败");
			}
		} else {
			$userInfo = D('ucenter_member a')->field('a.username,a.mobile,b.*')->join(" left join teach_institution b on a.id = b.uid")->where(array('a.id' => session('user_id'), 'a.user_type' => 3))->find();
			$this->assign('userInfo', $userInfo);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 图库列表
	 */
	public function pictureGalleryList()
	{
		if ($_REQUEST['name'] != '') {
			$map['b.name'] = array('like', '%' . $_REQUEST['name'] . '%');
			$this->assign('name', $_REQUEST['name']);
		}
		$count = M('picture_gallery a')->join(" left join teach_picture_gallery_type b on a.type_id = b.id")->where($map)->count();
		$Page = new \Think\Page($count, 20, array('name' => $_REQUEST['name']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('picture_gallery a')->field("a.*")->join(" left join teach_picture_gallery_type b on a.type_id = b.id")->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('a.id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '图片列表';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 密码修改
	 */
	public function updatePassword()
	{
		$uid = session('user_id');
		$oldPassword = $_REQUEST['oldPassword'];
		$newPassword = $_REQUEST['newPassword'];
		$confirmPassword = $_REQUEST['confirmPassword'];

		if (empty($oldPassword)) {
			$this->error("请输入原密码");
		}

		$member = D('ucenter_member')->where(array('id' => $uid))->find();
		if (think_ucenter_md5($oldPassword, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') != $member['password']) {
			$this->error("原密码输入错误");
		}

		if (empty($newPassword)) {
			$this->error("请输入新密码");
		}

		if (mb_strlen($newPassword, 'UTF-8') != 6) {
			$this->error("密码长度为6位");
		}

		if ($newPassword != $confirmPassword) {
			$this->error("您两次输入的密码不一致");
		}

		$result = D('ucenter_member')->where(array('id' => $uid))->save(array('password' => think_ucenter_md5($newPassword, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT')));
		if ($result) {
			$this->success("修改成功");
		} else {
			$this->error("修改失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 师资力量
	 */
	public function faculty()
	{
		$uid = session('user_id');
		//当前用户所在的机构
		$institution = D('institution')->where(array('uid' => $uid))->find();
		$map['iid'] = array('eq', $institution['id']);
		$count = M('teacher')->where($map)->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('teacher')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
		foreach ($list as $key => $item) {
			if ($item['section_ids'] != '') {
				$section_ids_str = mb_substr($item['section_ids'], mb_strlen($item['section_ids'], 'UTF-8') - 1, mb_strlen($item['section_ids'], 'UTF-8'), 'UTF-8');
				if ($section_ids_str == ',') {
					$item['section_ids'] = mb_substr($item['section_ids'], 0, mb_strlen($item['section_ids'], 'UTF-8') - 1, 'UTF-8');
				}
				$section_ids_arr = explode(",", $item['section_ids']);

				$section_name = "";
				for ($i = 0; $i < count($section_ids_arr); $i++) {
					$name = D('section_teacher')->where(array('id' => $section_ids_arr[$i]))->getField('name');
					$section_name .= $name ? $name . "," : '';
				}
				$list[$key]['section_name'] = $section_name;
			} else {
				$list[$key]['section_name'] = "";
			}
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '师资力量';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 添加师资力量
	 */
	public function addFaculty()
	{
		$uid = session('user_id');
		$memberInfo = D('ucenter_member')->where(array('id' => $uid))->find();
		if ($memberInfo['user_type'] != 3) {
			return api_return(2, '抱歉，只有机构负责人才能添加师资力量');
		}

		$data['name'] = $_REQUEST['name'];
		$data['tag'] = $_REQUEST['tag'];
		$data['desc'] = $_REQUEST['desc'];
		$section_ids_arr = $_REQUEST['section_ids'];
		$data['section_ids'] = "";
		foreach ($section_ids_arr as $key => $item) {
			$data['section_ids'] .= $section_ids_arr[$key] . ",";
		}

		$data['sign'] = $_REQUEST['sign'];

		if ($data['name'] == '') {
			return api_return(2, '请输入姓名');
		}

		if ($data['section_ids'] == '') {
			return api_return(2, '请选择板块');
		}

		$ucenterMemberData['username'] = $data['name'];
		$ucenterMemberData['password'] = think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
		$ucenterMemberData['iid'] = $memberInfo['iid'];
		$time = time();
		$ucenterMemberData['reg_time'] = $time;
		$ucenterMemberData['user_type'] = 4;
		$ucenterMemberData['update_time'] = $time;
		$ucenterMemberData['status'] = 1;
		$result = D('ucenter_member')->add($ucenterMemberData);
		if ($result) {
			$memberData['uid'] = $result;
			$memberData['nickname'] = $data['name'];
			$memberData['reg_time'] = $time;
			$memberData['status'] = 1;
			$res = D('member')->add($memberData);
			if ($res) {
				$data['create_time'] = $time;
				$data['update_time'] = $time;
				$resu = D('teacher')->add($data);
				if ($resu) {
					return api_return(1, '添加成功');
				} else {
					return api_return(2, '添加失败');
				}
			} else {
				return api_return(2, '添加失败');
			}
		} else {
			return api_return(2, '添加失败');
		}
	}

	/**
	 * Mr.Jiang
	 * 添加师资力量
	 */
	public function updateFaculty()
	{
		$uid = session('user_id');
		$memberInfo = D('ucenter_member')->where(array('id' => $uid))->find();
		if ($memberInfo['user_type'] != 3) {
			return api_return(2, '抱歉，只有机构负责人才能添加师资力量');
		}

		$id = $_REQUEST['id'];
		$data['name'] = $_REQUEST['name'];
		$data['tag'] = $_REQUEST['tag'];
		$data['desc'] = $_REQUEST['desc'];
		$section_ids_arr = $_REQUEST['section_ids'];
		$data['section_ids'] = "";
		foreach ($section_ids_arr as $key => $item) {
			$data['section_ids'] .= $section_ids_arr[$key] . ",";
		}

		$data['sign'] = $_REQUEST['sign'];

		if ($data['name'] == '') {
			return api_return(2, '请输入姓名');
		}

		if ($data['section_ids'] == '') {
			return api_return(2, '请选择板块');
		}

		$time = time();
		$data['update_time'] = $time;
		$resu = D('teacher')->where(array('id' => $id))->save($data);
		if ($resu) {
			return api_return(1, '编辑成功');
		} else {
			return api_return(2, '编辑失败');
		}
	}

	/**
	 * Mr.Jiang
	 * 获取所属板块
	 */
	public function getSectionTeacher()
	{
		$list = D('section_teacher')->order('id desc')->select();
		$list = $list ? $list : [];

		return api_return(1, '信息获取成功', $list);
	}

	/**
	 * Mr.Jiang
	 * 获取意见反馈标签
	 */
	public function getFeedbackTag()
	{
		$list = D('feedback_tag')->order('id desc')->select();
		$list = $list ? $list : [];

		return api_return(1, '信息获取成功', $list);
	}

	/**
	 * Mr.Jiang
	 * 提交意见反馈
	 */
	public function saveFeedback()
	{
		$data['uid'] = session('user_id');
		$data['type_name'] = $_REQUEST['type_name'];
		$data['content'] = $_REQUEST['content'];
		$data['type'] = 2;
		$time = time();
		$data['create_time'] = $time;
		$data['update_time'] = $time;
		$result = D('feedback')->add($data);
		if ($result) {
			return api_return(1, '提交成功');
		} else {
			return api_return(2, '提交失败');
		}
	}

	/**
	 * Mr.Jiang
	 * 根据身份证获取学生账号信息
	 */
	public function getAccount()
	{
		$id_card = $_REQUEST['id_card'];
		$id_account = D('ucenter_member')->where(array('user_type' => 2, 'id_card' => $id_card))->getField('id_account');
		$data['id_account'] = $id_account ? $id_account : '';

		return api_return(1, '信息获取成功', $data);
	}

	/**
	 * Mr.Jiang
	 * 根据类别获得分类
	 */
	public function getType()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		if ($category == 1) {
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else if ($category == 2) {
			$parent = D('video_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('video_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		} else {
			$parent = D('live_course_type')->where(array('pid' => 0))->order('id desc')->select();
			$child = D('live_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
		}
		$data['parent'] = $parent ? $parent : [];
		$data['child'] = $child ? $child : [];

		return api_return(1, '信息获取成功', $data);
	}

	/**
	 * Mr.Jiang
	 * 根据类别与父类获得子类
	 */
	public function getChildType()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$pid = $_REQUEST['pid'];
		if ($category == 1) {
			$child = D('face_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else if ($category == 2) {
			$child = D('video_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		} else {
			$child = D('live_course_type')->where(array('pid' => $pid))->order('id desc')->select();
		}

		return api_return(1, '信息获取成功', $child);
	}

	/**
	 * Mr.Jiang
	 * 根据类别与分类获取课程
	 */
	public function getCourse()
	{
		$category = $_REQUEST['category'];//1面授，2点播，3直播
		$type_id = $_REQUEST['type_id'];
		$iid = $_REQUEST['iid'];//机构ID
		if ($category == 1) {
			$course = D('face_course')->where(array('iid' => $iid, 'two_type_id' => $type_id))->order('id desc')->select();
		} else if ($category == 2) {
			$course = D('video_course')->where(array('iid' => $iid, 'two_type_id' => $type_id))->order('id desc')->select();
		} else {
			$course = D('live_course')->where(array('iid' => $iid, 'two_type_id' => $type_id))->order('id desc')->select();
		}

		return api_return(1, '信息获取成功', $course);
	}

	/**
	 * Mr.Jiang
	 * 课程拼团列表
	 */
	public function groupWork()
	{
		$count = M('group_work')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('group_work')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '课程拼团列表';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增拼团
	 */
	public function addGroupWork()
	{
		$iid = $_REQUEST['iid'];
		if (IS_POST) {
			$data['category'] = $_REQUEST['category'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['course_id'] = $_REQUEST['course_id'];
			$data['start_time'] = strtotime($_REQUEST['start_time']);
			$data['end_time'] = strtotime($_REQUEST['end_time']);
			$data['people_number'] = $_REQUEST['people_number'];
			$data['time_limit'] = $_REQUEST['time_limit'];
			$data['group_price'] = $_REQUEST['group_price'];
			$data['chief_price'] = $_REQUEST['chief_price'];
			$data['group_limit_number'] = $_REQUEST['group_limit_number'];

			if (empty($data['course_id'])) {
				$this->error("请选择课程");
			}

			if (empty($data['start_time'])) {
				$this->error("请选择拼团开始时间");
			}

			if (empty($data['end_time'])) {
				$this->error("请选择拼团结束时间");
			} else {
				if ($data['end_time'] < $data['start_time']) {
					$this->error("拼团结束时间不能小于拼团开始时间");
				} else {
					if ($data['end_time'] < time()) {
						$data['status'] = 2;
					} else {
						if ($data['start_time'] > time()) {
							$data['status'] = 0;
						} else {
							$data['status'] = 1;
						}
					}
				}
			}

			if ($data['people_number'] < 1) {
				$this->error("拼团人数至少一人");
			}

			if (empty($data['time_limit'])) {
				$this->error("请输入拼团时限");
			}

			if (empty($group_limit_number)) {
				$this->error("请输入限制成团数");
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('group_work')->add($data);
			if ($result) {
				$this->success("新增成功", U('groupWork'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
			//课程
			$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id']))->order('id desc')->select();

			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('course', $course);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 课程秒杀列表
	 */
	public function seckill()
	{
		$count = M('seckill')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('seckill')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '课程秒杀列表';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增秒杀
	 */
	public function addSeckill()
	{
		$iid = $_REQUEST['iid'];
		if (IS_POST) {
			$data['category'] = $_REQUEST['category'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['course_id'] = $_REQUEST['course_id'];
			$data['start_time'] = strtotime($_REQUEST['start_time']);
			$data['end_time'] = strtotime($_REQUEST['end_time']);
			$data['price'] = $_REQUEST['price'];

			if (empty($data['course_id'])) {
				$this->error("请选择课程");
			}

			if (empty($data['start_time'])) {
				$this->error("请选择拼团开始时间");
			}

			if (empty($data['end_time'])) {
				$this->error("请选择拼团结束时间");
			} else {
				if ($data['end_time'] < $data['start_time']) {
					$this->error("拼团结束时间不能小于拼团开始时间");
				} else {
					if ($data['end_time'] < time()) {
						$data['status'] = 2;
					} else {
						if ($data['start_time'] > time()) {
							$data['status'] = 0;
						} else {
							$data['status'] = 1;
						}
					}
				}
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('seckill')->add($data);
			if ($result) {
				$this->success("新增成功", U('seckill'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('face_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('face_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();
			//课程
			$course = D('face_course')->where(array('iid' => $iid, 'one_type_id' => $parent[0]['id'], 'two_type_id' => $child[0]['id']))->order('id desc')->select();

			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('course', $course);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 获取商品
	 */
	public function getGoods()
	{
		$iid = $_REQUEST['iid'];//机构
		$category = $_REQUEST['category'];//类别（1面授，2点播，3直播，4商品）
		if ($category == 1) {
			$list = D('face_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else if ($category == 2) {
			$list = D('video_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else if ($category == 3) {
			$list = D('live_course')->field('id,name')->where(array('iid' => $iid))->order('id desc')->select();
		} else {
			$list = D('goods')->field('id,name')->order('id desc')->select();
		}
		$list = $list ? $list : [];

		return api_return(1, '信息获取成功', $list);
	}

	/**
	 * Mr.Jiang
	 * 优惠券列表
	 */
	public function coupon()
	{
		$count = M('coupon')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('coupon')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '优惠券列表';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增优惠券
	 */
	public function addCoupon()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['name'] = $_REQUEST['name'];
			$data['goods_ids'] = $_REQUEST['goods_ids'];
			$data['count'] = $_REQUEST['count'];
			$data['get_day'] = $_REQUEST['get_day'];
			$data['min_price'] = $_REQUEST['min_price'];
			$data['deduct_price'] = $_REQUEST['deduct_price'];
			$data['use_day'] = $_REQUEST['use_day'];
			$data['get_count'] = $_REQUEST['get_count'];

			if ($data['name'] == '') {
				$this->error("请输入优惠券名称");
			}

			$time = time();
			$data['update_time'] = $time;
			$data['create_time'] = $time;
			$result = D('coupon')->add($data);
			if ($result) {
				$this->success("新增成功", U('coupon'));
			} else {
				$this->error("新增失败");
			}
		} else {
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 停止发券
	 */
	public function stopCoupons()
	{
		$id = $_REQUEST['id'];
		$coupon = D('coupon')->where(array('id' => $id))->find();
		if ($coupon['is_del'] == 1) {
			$this->error("优惠券已被删除");
		}

		if ($coupon['status'] == 1) {
			$this->error("优惠券已停止发券");
		}

		$expiration_time = $coupon['create_time'] + $coupon['use_day'] * 24 * 60 * 60;
		if ($expiration_time <= time()) {
			$this->error("该优惠券已结束");
		}

		$result = D('coupon')->where(array('id' => $id))->save(array('status' => 1));
		if ($result) {
			$this->success("操作成功");
		} else {
			$this->error("操作失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 直播课管理列表
	 */
	public function liveCourse()
	{
		$count = M('live_course')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('live_course')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '直播课管理';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新开直播课
	 */
	public function addLiveCourse()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['name'] = $_REQUEST['name'];
			$data['img'] = $_REQUEST['img'];
			$data['desc'] = $_REQUEST['desc'];
			$data['minute'] = $_REQUEST['minute'];
			$data['price'] = $_REQUEST['price'];
			$data['teacher_id'] = $_REQUEST['teacher_id'];
			$data['live_time_date'] = $_REQUEST['live_time_date'];
			$data['live_time'] = strtotime($_REQUEST['live_time_date']);
			$data['live_time_slot'] = $_REQUEST['live_time_slot'];

			$time = time();
			$data['update_time'] = $time;
			$data['create_time'] = $time;
			$result = D('live_course')->add($data);
			if ($result) {
				$this->error("新增成功", U('liveCourse'));
			} else {
				$this->error("新增失败");
			}
		} else {
			//父级分类
			$parent = D('live_course_type')->where(array('pid' => 0))->order('id desc')->select();
			//子级分类
			$child = D('live_course_type')->where(array('pid' => $parent[0]['id']))->order('id desc')->select();

			//老师
			$iid = $_REQUEST['iid'];
			$teach = D('ucenter_member')->where(array('user_type' => 4, 'iid' => $iid))->order('id desc')->select();

			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->assign('teach', $teach);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 面授课管理列表
	 */
	public function faceCourse()
	{
		$count = M('face_course')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('face_course')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '面授课管理';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新开面授课
	 */
	public function addFaceCourse()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['one_type_id'] = $_REQUEST['one_type_id'];
			$data['two_type_id'] = $_REQUEST['two_type_id'];
			$data['name'] = $_REQUEST['name'];
			$data['img'] = $_REQUEST['img'];
			$data['price'] = $_REQUEST['price'];
			$data['desc'] = $_REQUEST['desc'];
			$data['catalog'] = $_REQUEST['catalog'];
			$data['enlist_count'] = $_REQUEST['enlist_count'];
			$data['end_enlist_time'] = strtotime($_REQUEST['end_enlist_time']);
			$data['province_id'] = $_REQUEST['province_id'];
			$data['city_id'] = $_REQUEST['city_id'];
			$data['area_id'] = $_REQUEST['area_id'];
			$data['address'] = $_REQUEST['address'];
			$data['study_time'] = $_REQUEST['study_time'];
			$data['teacher'] = $_REQUEST['teacher'];

			if ($data['name'] == '') {
				$this->error("请输入课程名称");
			}

			if (empty($data['img'])) {
				$this->error("请上传封面");
			}

			$data['is_upper'] = 1;
			$time = time();
			$data['update_time'] = $time;
			$data['create_time'] = $time;
			$result = D('face_course')->add($data);
			if ($result) {
				$this->success("新增成功", U('faceCourse'));
			} else {
				$this->error("新增失败");
			}
		} else {
			$province = D('province')->select();
			$city = D('city')->where(array('province_id' => $province[0]['id']))->select();
			$area = D('area')->where(array('city_id' => $city[0]['city_id']))->select();

			//老师
			$iid = $_REQUEST['iid'];
			$teach = D('ucenter_member')->where(array('user_type' => 4, 'iid' => $iid))->order('id desc')->select();

			$this->assign('province', $province);
			$this->assign('city', $city);
			$this->assign('area', $area);
			$this->assign('teach', $teach);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 面授课上下架
	 */
	public function loadingAndUnloadingFaceCourse()
	{
		$id = $_REQUEST['id'];
		$is_upper = $_REQUEST['is_upper'];
		if ($is_upper == 1) {
			$msg = "上架";
		} else {
			$msg = "下架";
		}
		$result = D('face_course')->where(array('id' => $id))->save(array('is_upper' => $is_upper));
		if ($result) {
			$this->success($msg . "成功");
		} else {
			$this->error($msg . "失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 删除面授课
	 */
	public function delFaceCourse()
	{
		$id = $_REQUEST['id'];
		$faceCourse = D('face_course')->where(array('id' => $id))->find();
		if ($faceCourse['is_del'] == 1) {
			$this->error("当前数据已被删除");
		}
		$result = D('face_course')->where(array('id' => $id))->save(array('is_del' => 1));
		if ($result) {
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 视频课管理
	 */
	public function videoCourse()
	{
		$count = M('video_course')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('video_course')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '视频课管理';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 视频课上下架
	 */
	public function loadingAndUnloadingVideoCourse()
	{
		$id = $_REQUEST['id'];
		$is_upper = $_REQUEST['is_upper'];
		if ($is_upper == 1) {
			$msg = "上架";
		} else {
			$msg = "下架";
		}
		$result = D('video_course')->where(array('id' => $id))->save(array('is_upper' => $is_upper));
		if ($result) {
			$this->success($msg . "成功");
		} else {
			$this->error($msg . "失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 删除视频课
	 */
	public function delVideoCourse()
	{
		$id = $_REQUEST['id'];
		$faceCourse = D('video_course')->where(array('id' => $id))->find();
		if ($faceCourse['is_del'] == 1) {
			$this->error("当前数据已被删除");
		}
		$result = D('video_course')->where(array('id' => $id))->save(array('is_del' => 1));
		if ($result) {
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 内容统计
	 */
	public function contentStatistics()
	{
		$iid = $_REQUEST['iid'];
		//直播课统计，累计开播、预开播
		$cumulative_live_map['status'] = array(array('eq', 2), array('eq', 3), 'OR');
		$cumulative_live_map['is_del'] = array('eq', 0);
		$cumulative_live_map['iid'] = array('eq', $iid);
		$cumulative = D('live_course')->where($cumulative_live_map)->count();
		$this->assign('cumulative', $cumulative);

		$pre_live_map['status'] = array('eq', 1);
		$pre_live_map['is_del'] = array('eq', 0);
		$pre_live_map['iid'] = array('eq', $iid);
		$pre = D('live_course')->where($pre_live_map)->count();
		$this->assign('pre', $pre);

		//点播课统计，总课程数、购买数
		$total_video_map['is_del'] = array('eq', 0);
		$total_video_map['is_upper'] = array('eq', 1);
		$total_video_map['iid'] = array('eq', $iid);
		$total = D('video_course')->where($total_video_map)->count();
		$this->assign('total', $total);

		$order_map['a.status'] = array('eq', 4);
		$order_map['a.iid'] = array('eq', $iid);
		$order_map['a.type'] = array('eq', 3);
		$order = D('order a')->join('left join teach_vice_order b on a.id = b.order_id')->where($order_map)->count();
		$this->assign('order', $order);

		//面授课统计，开课数、报名数
		$class_map['iid'] = array('eq', $iid);
		$class_map['status'] = array('eq', 2);
		$class_map['is_upper'] = array('eq', 1);
		$class_map['is_del'] = array('eq', 0);
		$class = D('face_course')->where($class_map)->count();
		$this->assign('class', $class);

		$signup_map['a.status'] = array('eq', 4);
		$signup_map['a.iid'] = array('eq', $iid);
		$signup_map['a.type'] = array('eq', 2);
		$signup = D('order a')->join('left join teach_vice_order b on a.id = b.order_id')->where($signup_map)->count();
		$this->assign('signup', $signup);

		//折线图统计
		//面授

		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 交易管理
	 */
	public function order()
	{
		$iid = $_REQUEST['iid'];
		$map['a.iid'] = array('eq', $iid);

		if (!empty($_REQUEST['type'])) {
			$map['a.type'] = array('eq', $_REQUEST['type']);
			$this->assign('type', $_REQUEST['type']);
		}

		if ($_REQUEST['username'] != '') {
			$map['c.username'] = array('like', '%' . $_REQUEST['username'] . '%');
			$this->assign('username', $_REQUEST['username']);
		}

		if ($_REQUEST['id_card'] != '') {
			$map['c.id_card'] = array('like', '%' . $_REQUEST['id_card'] . '%');
			$this->assign('id_card', $_REQUEST['id_card']);
		}

		if ($_REQUEST['order_sn'] != '') {
			$map['a.order_sn'] = array('like', '%' . $_REQUEST['order_sn'] . '%');
			$this->assign('order_sn', $_REQUEST['order_sn']);
		}

		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'])), 'AND');
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		} else {
			if (!empty($_REQUEST['start_time'])) {
				$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
				$this->assign('start_time', $_REQUEST['start_time']);
			}
			if (!empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time']));
				$this->assign('end_time', $_REQUEST['end_time']);
			}
		}

		$count = D('order a')->join('left join teach_vice_order b on a.id = b.order_id')->join('left join teach_ucenter_member c on a.uid = c.id')->where($map)->group('a.id')->count();
		$Page = new \Think\Page($count, 10, array('type' => $_REQUEST['type'], 'username' => $_REQUEST['username'], 'id_card' => $_REQUEST['id_card'], 'order_sn' => $_REQUEST['order_sn'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('order a')->field('a.*,c.username,c.mobile,c.id_card')->join('left join teach_vice_order b on a.id = b.order_id')->join('left join teach_ucenter_member c on a.uid = c.id')->where($map)->group('a.id')->order('a.id desc')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
		foreach ($list as $key => $item) {
			//单价
			$price = D('vice_order')->where(array('order_id' => $item['id']))->SUM('price');
			$list[$key]['price'] = $price ? $price : 0;
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '交易管理';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 导出交易管理
	 */
	public function exportOrder()
	{

	}

	/**
	 * Mr.Jiang
	 * 交易管理详情
	 */
	public function orderDetails()
	{
		$id = $_REQUEST['id'];
		//订单信息
		$order = D('order')->where(array('id' => $id))->find();

		//用户信息
		$user = D('ucenter_member')->where(array('id' => $order['uid']))->find();

		//商品信息
		$list = D('vice_order')->where(array('order_id' => $id))->select();

		$this->assign('order', $order);
		$this->assign('user', $user);
		$this->assign('list', $list);

		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增收费
	 */
	public function addCharge()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['name'] = $_REQUEST['name'];
			$data['type'] = $_REQUEST['type'];
			$data['mobile'] = $_REQUEST['mobile'];
			$data['receivable'] = $_REQUEST['receivable'];
			$data['net_receipts'] = $_REQUEST['net_receipts'];
			$data['remarks'] = $_REQUEST['remarks'];
			$data['recruiters'] = $_REQUEST['recruiters'];
			$data['voucher'] = $_REQUEST['voucher'];

			if ($data['name'] == '') {
				$this->error("请输入姓名");
			}

			if ($data['mobile'] == '') {
				$this->error("请输入手机号");
			}

			if (empty($data['voucher'])) {
				$this->error("请上传收费凭证");
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('charge')->add($data);
			if ($result) {
				$this->success("新增成功", U('charge'));
			} else {
				$this->error("新增失败");
			}
		} else {
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 收费管理
	 */
	public function charge()
	{
		if ($_REQUEST['mobile'] != '') {
			$map['mobile'] = array('like', '%' . $_REQUEST['mobile'] . '%');
			$this->assign('mobile', $_REQUEST['mobile']);
		}

		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$map['create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'])), 'AND');
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		} else {
			if (!empty($_REQUEST['start_time'])) {
				$map['create_time'] = array('egt', strtotime($_REQUEST['start_time']));
				$this->assign('start_time', $_REQUEST['start_time']);
			}
			if (!empty($_REQUEST['end_time'])) {
				$map['create_time'] = array('elt', strtotime($_REQUEST['end_time']));
				$this->assign('end_time', $_REQUEST['end_time']);
			}
		}

		$count = D('charge')->where($map)->count();
		$Page = new \Think\Page($count, 10, array('mobile' => $_REQUEST['mobile'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('charge')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '收费管理';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 添加学生
	 */
	public function addStudent()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['username'] = $_REQUEST['username'];
			$data['id_card'] = $_REQUEST['id_card'];
			$data['sex'] = $_REQUEST['sex'];
			$data['age'] = $_REQUEST['age'];
			$data['mobile'] = $_REQUEST['mobile'];
			$data['person_charge'] = $_REQUEST['person_charge'];
			$data['source'] = $_REQUEST['source'];

			if ($data['username'] == '') {
				$this->error("请输入姓名");
			}

			if ($data['id_card'] == '') {
				$this->error("请输入身份证号");
			}

			$id_card_count = D('ucenter_member')->where(array('id_card' => $data['id_card']))->count();
			if ($id_card_count > 0) {
				$this->error("身份证号已存在");
			}

			if ($data['mobile'] == '') {
				$this->error("请输入手机号");
			}

			$mobile_count = D('ucenter_member')->where(array('mobile' => $data['mobile']))->count();
			if ($mobile_count > 0) {
				$this->error("手机号已存在");
			}

			$data['password'] = think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
			$data['user_type'] = 2;
			$time = time();
			$data['reg_time'] = $time;
			$data['update_time'] = $time;
			$data['status'] = 1;
			$result = D('ucenter_member')->add($data);
			if ($result) {
				$this->success("添加成功", U('student'));
			} else {
				$this->error("添加失败");
			}
		} else {
			//老师
			$iid = $_REQUEST['iid'];
			$teach = D('ucenter_member')->where(array('iid' => $iid, 'user_type' => 4))->order('id desc')->select();
			$this->assign('teach', $teach);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 班级分类
	 */
	public function classClassification()
	{
		$iid = $_REQUEST['iid'];
		$map['cc_id'] = array('eq', 0);
		$map['iid'] = array('eq', $iid);
		$count = D('class_classification')->where($map)->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('class_classification')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('sort asc')->select();
		foreach ($list as $key => $item) {
			$child = D('class_classification')->where(array('pid' => $item['id']))->order('sort asc')->select();
			$list[$key]['child'] = $child ? $child : [];
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '班级分类';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增或修改类目
	 */
	public function saveClassClassification()
	{
		$id = $_REQUEST['id'];
		$data['iid'] = $_REQUEST['iid'];
		$data['cc_id'] = $_REQUEST['cc_id'];
		$data['name'] = $_REQUEST['name'];
		$data['sort'] = $_REQUEST['sort'];

		if ($data['name'] == '') {
			return api_return(2, '请输入类目名称');
		}

		$time = time();
		$data['update_time'] = $time;
		if (empty($id)) {
			$data['create_time'] = $time;
			$result = D('class_classification')->add($data);
			if ($result) {
				return api_return(1, '新增成功');
			} else {
				return api_return(2, '新增失败');
			}
		} else {
			$result = D('class_classification')->where(array('id' => $id))->save($data);
			if ($result) {
				return api_return(1, '编辑成功');
			} else {
				return api_return(2, '编辑失败');
			}
		}
	}

	/**
	 * Mr.Jiang
	 * 删除类目
	 */
	public function delClassClassification()
	{
		$id = $_REQUEST['id'];
		$count = D('class_classification')->where(array('id' => $id))->count();
		if ($count <= 0) {
			$this->error("当前类目不存在或已被删除");
		}

		$cc_id_count = D('class_classification')->where(array('cc_id' => $id))->count();
		if ($cc_id_count > 0) {
			$this->error("抱歉，请先删除子类目");
		}

		$result = D('class_classification')->where(array('id' => $id))->delete();
		if ($result) {
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 班级列表
	 */
	public function classList()
	{
		$map['iid'] = array('eq', $_REQUEST['iid']);
		if ($_REQUEST['name'] != '') {
			$map['name'] = array('like', '%' . $_REQUEST['name'] . '%');
			$this->assign('name', $_REQUEST['name']);
		}

		if (!empty($_REQUEST['one_class_id'])) {
			$map['one_class_id'] = array('eq', $_REQUEST['one_class_id']);
			$this->assign('one_class_id', $_REQUEST['one_class_id']);
		}

		if (!empty($_REQUEST['two_class_id'])) {
			$map['two_class_id'] = array('eq', $_REQUEST['two_class_id']);
			$this->assign('two_class_id', $_REQUEST['two_class_id']);
		}

		if (!empty($_REQUEST['status'])) {
			$map['status'] = array('eq', $_REQUEST['status']);
			$this->assign('status', $_REQUEST['status']);
		}

		if (!empty($_REQUEST['start_time'])) {
			$map['start_time'] = array('eq', $_REQUEST['start_time']);
			$this->assign('start_time', $_REQUEST['start_time']);
		}

		if (!empty($_REQUEST['end_time'])) {
			$map['end_time'] = array('eq', $_REQUEST['end_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		}

		$count = D('class')->where($map)->count();
		$Page = new \Think\Page($count, 10, array('name' => $_REQUEST['name'], 'one_class_id' => $_REQUEST['one_class_id'], 'two_class_id' => $_REQUEST['two_class_id'], 'status' => $_REQUEST['status'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('class')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
		foreach ($list as $key => $item) {
			$parent = D('class_classification')->where(array('id' => $item['one_class_id']))->getField('name');
			$child = D('class_classification')->where(array('id' => $item['two_class_id']))->getField('name');
			$list[$key]['parent'] = $parent ? $parent : '';
			$list[$key]['child'] = $child ? $child : '';
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '班级列表';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增或修改班级
	 */
	public function saveClass()
	{
		if (IS_POST) {
			$id = $_REQUEST['id'];
			$data['iid'] = $_REQUEST['iid'];
			$data['name'] = $_REQUEST['name'];
			$data['one_class_id'] = $_REQUEST['one_class_id'];
			$data['two_class_id'] = $_REQUEST['two_class_id'];
			$data['status'] = $_REQUEST['status'];
			$data['start_time'] = strtotime($_REQUEST['start_time']);
			$data['end_time'] = strtotime($_REQUEST['end_time']);

			if ($data['name'] == '') {
				$this->error("请输入班级名称");
			}

			if (empty($data['one_class_id'])) {
				$this->error("请选择一级分类");
			}

			if (empty($data['two_class_id'])) {
				$this->error("请选择二级分类");
			}

			if (empty($data['start_time'])) {
				$this->error("请选择开班时间");
			}

			if (empty($data['end_time'])) {
				$this->error("请选择结束时间");
			}

			if ($data['start_time'] > $data['end_time']) {
				$this->error("结束时间不能小于开班时间");
			}

			$time = time();
			$data['update_time'] = $time;

			if (empty($id)) {
				$data['create_time'] = $time;
				$result = D('class')->add($data);
				if ($result) {
					$this->success("新增成功", U('classList'));
				} else {
					$this->error("新增失败");
				}
			} else {
				$result = D('class')->where(array('id' => $id))->save($data);
				if ($result) {
					$this->success("编辑成功", U('classList'));
				} else {
					$this->error("编辑失败");
				}
			}
		} else {
			$iid = $_REQUEST['iid'];
			//一级分类
			$parent = D('class_classification')->where(array('iid' => $iid, 'cc_id' => 0))->order('sort asc')->select();

			//二级分类
			$child = D('class_classification')->where(array('iid' => $iid, 'cc_id' => $parent[0]['id']))->order('sort asc')->select();

			$this->assign('parent', $parent);
			$this->assign('child', $child);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 根据班级一级分类获取二级分类
	 */
	public function getClassChild()
	{
		$cc_id = $_REQUEST['cc_id'];
		$child = D('class_classification')->where(array('cc_id' => $cc_id))->order('sort asc')->select();
		$child = $child ? $child : [];

		return api_return(1, '信息获取成功', $child);
	}

	/**
	 * Mr.Jiang
	 * 客户列表
	 */
	public function customer()
	{
		$iid = session('iid');
		$map['iid'] = $iid;

		if ($_REQUEST['keyword'] != '') {
			$where['name'] = array('like', '%' . $_REQUEST['keyword'] . '%');
			$where['mobile'] = array('like', '%' . $_REQUEST['keyword'] . '%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			$this->assign('keyword', $_REQUEST['keyword']);
		}

		if (!empty($_REQUEST['stage'])) {
			$map['stage'] = array('eq', $_REQUEST['stage']);
			$this->assign('stage', $_REQUEST['stage']);
		}

		if (!empty($_REQUEST['source'])) {
			$map['source'] = array('eq', $_REQUEST['source']);
			$this->assign('source', $_REQUEST['source']);
		}

		if (!empty($_REQUEST['degree'])) {
			$map['degree'] = array('eq', $_REQUEST['degree']);
			$this->assign('degree', $_REQUEST['degree']);
		}
		$count = D('customer')->where($map)->count();
		$Page = new \Think\Page($count, 10, array('keyword' => $_REQUEST['keyword'], 'stage' => $_REQUEST['stage'], 'source' => $_REQUEST['source'], 'degree' => $_REQUEST['degree']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('customer')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
		foreach ($list as $key => $item) {
			$username = D('Ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$list[$key]['username'] = $username ? $username : [];

			$follow_up = D('follow_up')->where(array('iid' => $iid, 'type' => 1, 'cid' => $item['id']))->order('create_time desc')->limit(1)->find();
			$list[$key]['last_time'] = $follow_up['create_time'] ? date('Y-m-d H:i', $follow_up['create_time']) : '-';

			//自己查看的次数
			$this_see = D('customer_see')->where(array('iid' => $iid, 'cid' => $item['id'], 'uid' => session('user_id')))->count();
			$list[$key]['this_see'] = $this_see ? $this_see : 0;
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->assign('cur', '客户列表');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 获取IP
	 */
	public function getIP()
	{
		static $realip;
		if (isset($_SERVER)) {
			if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
				$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$realip = $_SERVER["HTTP_CLIENT_IP"];
			} else {
				$realip = $_SERVER["REMOTE_ADDR"];
			}
		} else {
			if (getenv("HTTP_X_FORWARDED_FOR")) {
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			} else if (getenv("HTTP_CLIENT_IP")) {
				$realip = getenv("HTTP_CLIENT_IP");
			} else {
				$realip = getenv("REMOTE_ADDR");
			}
		}

		return $realip;
	}

	/**
	 * Mr.Jiang
	 * 客户详情
	 */
	public function customerDetails()
	{
		$id = $_REQUEST['id'];

		if (empty($_REQUEST['p']) && empty($_REQUEST['mode'])) {
			D('customer')->where(array('id' => $id))->setInc('see', 1);

			$customer_see_map['iid'] = session("iid");
			$customer_see_map['cid'] = $id;
			$customer_see_map['uid'] = session('user_id');
			$customer_see_map['ip'] = $this->getIP();
			$customer_see_map['create_time'] = time();
			D('customer_see')->add($customer_see_map);
		}

		//客户信息
		$info = D('customer')->where(array('id' => $id))->find();
		$customer_path = D('picture')->where(array('id' => $info['cover_id']))->getField('path');
		$info['customer_path'] = $customer_path ? $customer_path : '';
		$info['remarks'] = $info['remarks'] ? $info['remarks'] : '暂无';

		//线索信息
		$clue = D('clue')->where(array('mobile' => $info['mobile']))->order('create_time desc')->limit(1)->find();

		//跟进信息
		$qq_count = D('follow_up')->where(array('cid' => $info['id'], 'type' => 1, 'mode' => 1))->count();
		$wx_count = D('follow_up')->where(array('cid' => $info['id'], 'type' => 1, 'mode' => 2))->count();
		$sms_count = D('follow_up')->where(array('cid' => $info['id'], 'type' => 1, 'mode' => 3))->count();
		$bf_count = D('follow_up')->where(array('cid' => $info['id'], 'type' => 1, 'mode' => 4))->count();
		$other_count = D('follow_up')->where(array('cid' => $info['id'], 'type' => 1, 'mode' => 5))->count();

		$follow_up_map['cid'] = array('eq', $info['id']);
		$follow_up_map['type'] = array('eq', 1);
		if (!empty($_REQUEST['mode']) && $_REQUEST['mode'] != 'all') {
			$follow_up_map['mode'] = array('eq', $_REQUEST['mode']);
		}
		$count = D('follow_up')->where($follow_up_map)->count();
		$Page = new \Think\Page($count, 5, array('id' => $id, 'mode' => $_REQUEST['mode']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$follow_up = D('follow_up')->where($follow_up_map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
		foreach ($follow_up as $key => $item) {
			$gjz_name = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$follow_up[$key]['gjz_name'] = $gjz_name ? $gjz_name : '';
		}

		$this->assign('info', $info);
		$this->assign('clue', $clue);
		$this->assign('qq_count', $qq_count);
		$this->assign('wx_count', $wx_count);
		$this->assign('sms_count', $sms_count);
		$this->assign('bf_count', $bf_count);
		$this->assign('other_count', $other_count);
		$this->assign('follow_up', $follow_up);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign('mode', $_REQUEST['mode']);
		$this->assign('cur', '客户列表');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增客户
	 */
	public function addCustomer()
	{
		if (IS_POST) {
			$data['uid'] = session('user_id');
			$data['iid'] = session('iid');
			$data['name'] = $_REQUEST['name'];
			$data['mobile'] = $_REQUEST['mobile'];
			$data['qq'] = $_REQUEST['qq'];
			$data['wechat'] = $_REQUEST['wechat'];
			$data['id_card'] = $_REQUEST['id_card'];
			$data['source'] = $_REQUEST['source'];
			$data['degree'] = $_REQUEST['degree'];
			$data['cover_id'] = $_REQUEST['cover_id'];
			$data['remarks'] = $_REQUEST['remarks'];

			if ($data['name'] == '') {
				$this->error("请输入客户姓名");
			}

			if ($data['mobile'] == '') {
				$this->error("请输入手机号码");
			}

			$mobile_count = D('customer')->where(array('mobile' => $data['mobile']))->count();
			if ($mobile_count > 0) {
				$this->error("抱歉，您输入的手机号已存在");
			}

			if ($data['id_card'] == '') {
				$this->error("请输入身份证号码");
			}

			$id_card_count = D('customer')->where(array('id_card' => $data['id_card']))->count();
			if ($id_card_count > 0) {
				$this->error("抱歉，您输入的身份证号码已存在");
			}

			$time = time();
			$data['create_time'] = $time;
			$data['update_time'] = $time;
			$result = D('customer')->add($data);
			if ($result) {
				if (session('Supertube') == 2) {
					//不记录超管的 只记录这种有权限的
					$data['account_id'] = session('user_id');
					$data['create_time'] = time();
					$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
					$data['content'] = '客户列表' . '-' . '新增客户';
					M('account_log')->add($data);
				}
				$this->success("新增客户成功", U('customer'));
			} else {
				$this->error("新增客户失败");
			}
		} else {
			$this->assign('cur', '客户列表');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 添加跟进
	 */
	public function addFollowUp()
	{
		if (IS_POST) {
			$data['iid'] = session("iid");
			$data['uid'] = session('user_id');
			$data['type'] = $_REQUEST['type'];
			$data['cid'] = $_REQUEST['cid'];
			$data['mode'] = $_REQUEST['mode'];
			if (empty($data['mode'])) {
				$this->error("请选择跟进方式");
			}
			$data['contact_time'] = strtotime($_REQUEST['contact_time']);
			if (empty($_REQUEST['contact_time'])) {
				$this->error("请选择联系时间");
			}
			$data['stage'] = $_REQUEST['stage'];
			if (empty($data['stage'])) {
				$this->error("请选择客户阶段");
			}
			$data['degree'] = $_REQUEST['degree'];
			if (empty($data['degree'])) {
				$this->error("请选择程度");
			}
			$data['content'] = $_REQUEST['content'];
			if ($data['content'] == '') {
				$this->error("请输入跟进内容");
			}
			$data['create_time'] = time();
			$result = D('follow_up')->add($data);
			if ($result) {
				$log_data['iid'] = session("iid");
				$log_data['uid'] = session('user_id');
				$log_data['create_time'] = time();
				if ($data['type'] == 1) {
					//客户联系次数加1
					D('customer')->where(array('id' => $data['cid']))->setInc('contact_number', 1);
					D('customer')->where(array('id' => $data['cid']))->save(array('stage' => $data['stage']));
					$log_data['customer'] = 1;
					D('follow_up_log')->add($log_data);
					if (session('Supertube') == 2) {
						//不记录超管的 只记录这种有权限的
						$data['account_id'] = session('user_id');
						$data['create_time'] = time();
						$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
						$data['content'] = '线索列表' . '-' . '添加跟进';
						M('account_log')->add($data);
					}
					$this->success("添加成功", U('customer'));
				} else {
					$log_data['clue'] = 1;
					D('follow_up_log')->add($log_data);
					if (session('Supertube') == 2) {
						//不记录超管的 只记录这种有权限的
						$data['account_id'] = session('user_id');
						$data['create_time'] = time();
						$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
						$data['content'] = '线索列表' . '-' . '添加跟进';
						M('account_log')->add($data);
					}
					$this->success("添加成功", U('clueList'));
				}
			} else {
				$this->error("添加失败");
			}
		} else {
			$this->assign('cid', $_REQUEST['cid']);
			if ($_REQUEST['type'] == 1) {
				$this->assign('cur', '客户列表');
			} else {
				$this->assign('cur', '线索列表');
			}
			$this->assign('type', $_REQUEST['type']);
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 删除客户
	 */
	public function delCustomer()
	{
		$id = $_REQUEST['id'];
		$count = D('customer')->where(array('id' => $id))->count();
		if ($count <= 0) {
			$this->error("当前数据不存在或已被删除");
		}

		$result = D('customer')->where(array('id' => $id))->delete();
		if ($result) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '客户列表' . '-' . '删除客户';
				M('account_log')->add($data);
			}
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 线索列表
	 */
	public function clueList()
	{
		$iid = session('iid');
		$map['iid'] = $iid;

		if ($_REQUEST['keyword'] != '') {
			$where['name'] = array('like', '%' . $_REQUEST['keyword'] . '%');
			$where['mobile'] = array('like', '%' . $_REQUEST['keyword'] . '%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			$this->assign('keyword', $_REQUEST['keyword']);
		}

		if (!empty($_REQUEST['status'])) {
			$map['status'] = array('eq', $_REQUEST['status']);
			$this->assign('status', $_REQUEST['status']);
		}

		if (!empty($_REQUEST['source'])) {
			$map['source'] = array('eq', $_REQUEST['source']);
			$this->assign('source', $_REQUEST['source']);
		}
		$count = D('clue')->where($map)->count();
		$Page = new \Think\Page($count, 10, array('keyword' => $_REQUEST['keyword'], 'status' => $_REQUEST['status'], 'source' => $_REQUEST['source']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('clue')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
		foreach ($list as $key => $item) {
			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$list[$key]['username'] = $username ? $username : '';
			$list[$key]['name'] = M('ucenter_member')->where("id=$item[distributor_id]")->getField('username');
			$follow_up = D('follow_up')->where(array('iid' => $iid, 'type' => 2, 'cid' => $item['id']))->order('create_time desc')->limit(1)->find();
			$list[$key]['last_content'] = $follow_up['content'] ? $follow_up['content'] : '-';

			//自己查看的次数
			$this_see = D('clue_see')->where(array('iid' => $iid, 'cid' => $item['id'], 'uid' => session('user_id')))->count();
			$list[$key]['this_see'] = $this_see ? $this_see : 0;
		}
		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$teach = D('ucenter_member')->where(array('iid' => $iid, 'user_type' => 4))->order('id desc')->select();
		$this->assign('teach', $teach);

		$this->assign('cur', '线索列表');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 线索详情
	 */
	public function clueDetails()
	{
		$id = $_REQUEST['id'];

		if (empty($_REQUEST['p']) && empty($_REQUEST['mode'])) {
			D('clue')->where(array('id' => $id))->setInc('see', 1);

			$clue_see_map['iid'] = session("iid");
			$clue_see_map['cid'] = $id;
			$clue_see_map['uid'] = session('user_id');
			$clue_see_map['ip'] = $this->getIP();
			$clue_see_map['create_time'] = time();
			D('clue_see')->add($clue_see_map);
		}

		//基本信息
		$info = D('clue')->where(array('id' => $id))->find();
		$this->assign('info', $info);

		$map['cid'] = array('eq', $id);
		$map['type'] = array('eq', 2);
		if (!empty($_REQUEST['mode']) && $_REQUEST['mode'] != 'all') {
			$map['mode'] = array('eq', $_REQUEST['mode']);
		}
		$count = M('follow_up')->where($map)->count();
		$Page = new \Think\Page($count, 5, array('id' => $id, 'mode' => $_REQUEST['mode']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('follow_up')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
		foreach ($list as $key => $item) {
			$gjz_name = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$list[$key]['gjz_name'] = $gjz_name ? $gjz_name : '';
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);
		$this->assign('mode', $_REQUEST['mode']);

		//QQ
		$qq_map['cid'] = array('eq', $id);
		$qq_map['type'] = array('eq', 2);
		$qq_map['mode'] = array('eq', 1);
		$qq_count = M('follow_up')->where($qq_map)->count();
		$this->assign('qq_count', $qq_count);

		//微信
		$wx_map['cid'] = array('eq', $id);
		$wx_map['type'] = array('eq', 2);
		$wx_map['mode'] = array('eq', 2);
		$wx_count = M('follow_up')->where($wx_map)->count();
		$this->assign('wx_count', $wx_count);

		//短信
		$sms_map['cid'] = array('eq', $id);
		$sms_map['type'] = array('eq', 2);
		$sms_map['mode'] = array('eq', 3);
		$sms_count = M('follow_up')->where($sms_map)->count();
		$this->assign('sms_count', $sms_count);

		//拜访
		$bf_map['cid'] = array('eq', $id);
		$bf_map['type'] = array('eq', 2);
		$bf_map['mode'] = array('eq', 4);
		$bf_count = M('follow_up')->where($bf_map)->count();
		$this->assign('bf_count', $bf_count);

		//其他
		$other_map['cid'] = array('eq', $id);
		$other_map['type'] = array('eq', 2);
		$other_map['mode'] = array('eq', 5);
		$other_count = M('follow_up')->where($other_map)->count();
		$this->assign('other_count', $other_count);

		$this->assign('cur', '线索列表');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 新增线索
	 */
	public function addClue()
	{
		if (IS_POST) {
			$data['iid'] = session('iid');
			$data['uid'] = session('user_id');
			$data['name'] = $_REQUEST['name'];
			$data['mobile'] = $_REQUEST['mobile'];
			$data['qq'] = $_REQUEST['qq'];
			$data['wechat'] = $_REQUEST['wechat'];
			$data['source'] = $_REQUEST['source'];
			$data['status'] = $_REQUEST['status'];

			if ($data['name'] == '') {
				$this->error("请输入线索名称");
			}

			if ($data['mobile'] == '') {
				$this->error("请输入手机号");
			}

			$time = time();
			$data['create_time'] = $time;
			$result = D('clue')->add($data);
			if ($result) {
				if (session('Supertube') == 2) {
					//不记录超管的 只记录这种有权限的
					$data['account_id'] = session('user_id');
					$data['create_time'] = time();
					$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
					$data['content'] = '线索列表' . '-' . '新增线索';
					M('account_log')->add($data);
				}
				$this->success("新增成功", U('clueList'));
			} else {
				$this->error("新增失败");
			}
		} else {
			$this->assign('cur', '线索列表');
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 删除线索
	 */
	public function delClue()
	{
		$id = $_REQUEST['id'];
		$count = D('clue')->where(array('id' => $id))->count();
		if ($count <= 0) {
			$this->error("当前数据不存在或已被删除");
		}

		$result = D('clue')->where(array('id' => $id))->delete();
		if ($result) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '线索列表' . '-' . '删除线索';
				M('account_log')->add($data);
			}
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 线索分配
	 */
	public function distribution()
	{
		$id = $_REQUEST['id'];
		$distributor_id = $_REQUEST['distributor_id'];
		if (empty($distributor_id)) {
			return api_return(2, '请选择要分配的人员');
		}

		$result = D('clue')->where(array('id' => $id))->save(array('distributor_id' => $distributor_id, 'distributor_time' => time()));
		if ($result) {
			if (session('Supertube') == 2) {
				//不记录超管的 只记录这种有权限的
				$data['account_id'] = session('user_id');
				$data['create_time'] = time();
				$data['ip_address'] = $_SERVER["REMOTE_ADDR"];
				$data['content'] = '线索列表' . '-' . '分配线索';
				M('account_log')->add($data);
			}

			return api_return(1, '分配成功');
		} else {
			return api_return(2, '分配失败');
		}
	}

	//自动分配
	public function automatic_allocation()
	{
		$iid = session('iid');
		$people = $_POST['people'];
		$number = $_POST['number'];
		$type = $_POST['type'];
		if ($type == 1) {
			//按员工顺序分配
			$list = M('ucenter_member')->where("iid=$iid && user_type=4")->order('id desc')->limit(0, $people)->field('id')->select();
		} else {
			//按员工手头客户从少到多分配
			$list = M('ucenter_member')->where("iid=$iid && user_type=4")->field('id')->select();
			$volume2 = [];
			foreach ($list as $k => $v) {
				$volume2[$k] = M('clue')->where("distributor_id=$v[id]")->count();
			}

			array_multisort($volume2, SORT_ASC, $list);
		}

		//结果集分页
		$list = array_slice($list, 0, $people);
		$clue_list = M('clue')->where("iid=$iid")->order('id asc')->select();
		$b = 0;
		foreach ($list as $k => $v) {

			for ($i = 0; $i < $number; $i++) {
				//多少人一个轮回 一人被分配几条线索
				$id = $clue_list[$b]['id'];
				if ($id) {
					M('clue')->where("id=$id")->save(['distributor_id' => $v['id'], 'distributor_time' => time()]);
					$b++;
				} else {
					//线索分完了
					$value = ['code' => '100', 'msg' => '成功'];
					exit(json_encode($value));
				}
			}
		}
		$value = ['code' => '100', 'msg' => '成功'];
		exit(json_encode($value));
	}

	/**
	 * Mr.Jiang
	 * 获取同机构下的老师
	 */
	public function getTeach()
	{
		$iid = session('iid');
		$teach = D('ucenter_member')->where(array('iid' => $iid, 'user_type' => 4))->order('id desc')->select();
		$teach = $teach ? $teach : [];

		return api_return(1, '信息获取成功', $teach);
	}

	/**
	 * Mr.Jiang
	 * 排行榜
	 */
	public function rankingList()
	{
		$iid = session('iid');
		$rank_type = $_REQUEST['rank_type'];
		if ($rank_type == 1) {
			$map['a.iid'] = array('eq', $iid);
			$map['a.obj_id'] = array('gt', 0);
			$where = "a.iid = {$iid} and a.obj_id > 0";

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$where .= " and b.username like '%" . $_REQUEST['name'] . "%'";
				$this->assign('name', $_REQUEST['name']);
			}

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$where .= " and a.create_time >= " . strtotime($_REQUEST['start_time']) . " and a.create_time <= " . strtotime($_REQUEST['end_time'] . " 23:59:59");
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$where .= " and a.create_time >= " . strtotime($_REQUEST['start_time']);
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$where .= " and a.create_time <= " . strtotime($_REQUEST['end_time'] . " 23:59:59");
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			//直播课学员总数
			$live_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(live) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$live = $live_arr[0]['tp_sum'];
			//点播课学员总数
			$video_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(video) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$video = $video_arr[0]['tp_sum'];
			//面授课学员总数
			$face_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(face) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$face = $face_arr[0]['tp_sum'];

			$this->assign('live', $live);
			$this->assign('video', $video);
			$this->assign('face', $face);

			$count_arr = M('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->select();
			$count = count($count_arr);
			$Page = new \Think\Page($count, 5, array('rank_type' => $rank_type, 'name' => $_REQUEST['name'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = D('signup_ranking a')->field('a.obj_id as id,SUM(a.face) as face,SUM(a.live) as live,SUM(a.video) as video,SUM(a.face)+SUM(a.live)+SUM(a.video) as total,b.username')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->order('SUM(a.face)+SUM(a.live)+SUM(a.video)')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		} else if ($rank_type == 2) {
			$map['a.iid'] = array('eq', $iid);
			$map['a.uid'] = array('gt', 0);

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$this->assign('name', $_REQUEST['name']);
			}

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			//客户总数
			$customer_total_count = 0;

			//线索总数
			$clue_total_count = 0;

			//客户跟进总数
			$gj_total_customer = 0;

			//线索跟进总数
			$gj_total_clue = 0;

			$count_list = D('follow_up_log a')->join('left join teach_ucenter_member b on b.id = a.uid')->where($map)->group("a.uid")->select();
			$count = count($count_list);
			$Page = new \Think\Page($count, 5, array('rank_type' => $rank_type, 'name' => $_REQUEST['name'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));
			$list = D('follow_up_log a')->field('a.uid,SUM(a.customer) as customer,SUM(a.clue) as clue,b.username')->join('left join teach_ucenter_member b on b.id = a.uid')->where($map)->group('a.uid')->order("SUM(a.customer)+SUM(a.clue) desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
			foreach ($list as $key => $item) {
				//线索数量
				$clue_map2['iid'] = array('eq', $iid);
				$clue_where2['uid'] = array('eq', $item['uid']);
				$clue_where2['distributor_id'] = array('eq', $item['uid']);
				$clue_where2['_logic'] = 'or';
				$clue_map2['_complex'] = $clue_where2;
				$clue_count = D('clue')->where($clue_map2)->count();
				$list[$key]['clue_count'] = $clue_count ? $clue_count : 0;

				$clue_total_count = $clue_total_count + $clue_count;

				//客户数量
				$customer_map2['iid'] = array('eq', $iid);
				$customer_map2['uid'] = array('eq', $item['uid']);
				$customer_count = D('customer')->where($customer_map2)->count();
				$list[$key]['customer_count'] = $customer_count ? $customer_count : 0;

				$customer_total_count = $customer_total_count + $customer_count;

				$gj_total_customer = $gj_total_customer + $item['customer'];

				$gj_total_clue = $gj_total_clue + $item['clue'];

				//线索与客户联系数总和
				$list[$key]['total_contact_number'] = $item['customer'] + $item['clue'];

				//线索与客户总和
				$list[$key]['total_count'] = $list[$key]['clue_count'] + $list[$key]['customer_count'];
			}

			$this->assign('customer_count', $customer_total_count);

			$this->assign('clue_count', $clue_total_count);

			$this->assign('gj_customer', $gj_total_customer);

			$this->assign('gj_clue', $gj_total_clue);

			$gj_total = $gj_total_customer + $gj_total_clue;
			$this->assign('gj_total', $gj_total);

			$total = $customer_total_count + $clue_total_count;
			$this->assign('total', $total);
		} else {
			$map['a.iid'] = $iid;
			$map['a.is_pay'] = array('eq', 1);
			$map['a.type'] = array(array('eq', 1), array('eq', 2), array('eq', 3), 'OR');

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'] . " 00:00:00")), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time'] . " 00:00:00"));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$this->assign('name', $_REQUEST['name']);
			}
			$count_arr = D('order a')->where($map)->select();
			$count = count($count_arr);
			$Page = new \Think\Page($count, 10, array('rank_type' => $rank_type, 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time'], 'name' => $_REQUEST['name']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = D('order a')->field('a.id,a.pay_price,b.username')->join('left join teach_ucenter_member b on a.uid = b.id')->where($map)->order('a.pay_price desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->assign('rank_type', $rank_type);

		$this->assign('cur', '排行榜');
		$this->display();
	}

	public function rankingList123()
	{
		$iid = session('iid');
		$rank_type = $_REQUEST['rank_type'];
		if ($rank_type == 1) {
			$map['a.iid'] = array('eq', $iid);
			$map['a.obj_id'] = array('gt', 0);
			$where = "a.iid = {$iid} and a.obj_id > 0";

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$where .= " and b.username like '%" . $_REQUEST['name'] . "%'";
				$this->assign('name', $_REQUEST['name']);
			}

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$where .= " and a.create_time >= " . strtotime($_REQUEST['start_time']) . " and a.create_time <= " . strtotime($_REQUEST['end_time'] . " 23:59:59");
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$where .= " and a.create_time >= " . strtotime($_REQUEST['start_time']);
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$where .= " and a.create_time <= " . strtotime($_REQUEST['end_time'] . " 23:59:59");
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			//直播课学员总数
			$live_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(live) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$live = $live_arr[0]['tp_sum'];
			//点播课学员总数
			$video_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(video) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$video = $video_arr[0]['tp_sum'];
			//面授课学员总数
			$face_arr = D()->query(
				"select SUM(c.tp_SUM) as tp_sum from (SELECT SUM(face) AS tp_SUM FROM teach_signup_ranking a 
left join teach_ucenter_member b on b.id = a.obj_id 
WHERE {$where} GROUP BY a.obj_id) c"
			);
			$face = $face_arr[0]['tp_sum'];

			$this->assign('live', $live);
			$this->assign('video', $video);
			$this->assign('face', $face);

			$count_arr = M('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->select();
			$count = count($count_arr);
			$Page = new \Think\Page($count, 5, array('rank_type' => $rank_type, 'name' => $_REQUEST['name'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = D('signup_ranking a')->field('a.obj_id as id,SUM(a.face) as face,SUM(a.live) as live,SUM(a.video) as video,SUM(a.face)+SUM(a.live)+SUM(a.video) as total,b.username')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->order('SUM(a.face)+SUM(a.live)+SUM(a.video)')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		} else if ($rank_type == 2) {
			$map['a.iid'] = array('eq', $iid);
			$map['a.uid'] = array('gt', 0);

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$this->assign('name', $_REQUEST['name']);
			}

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			//客户总数
			$customer_total_count = 0;

			//线索总数
			$clue_total_count = 0;

			//客户跟进总数
			$gj_total_customer = 0;

			//线索跟进总数
			$gj_total_clue = 0;

			$count_list = D('follow_up_log a')->join('left join teach_ucenter_member b on b.id = a.uid')->where($map)->group("a.uid")->select();
			$count = count($count_list);
			$Page = new \Think\Page($count, 5, array('rank_type' => $rank_type, 'name' => $_REQUEST['name'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));
			$list = D('follow_up_log a')->field('a.uid,SUM(a.customer) as customer,SUM(a.clue) as clue,b.username')->join('left join teach_ucenter_member b on b.id = a.uid')->where($map)->group('a.uid')->order("SUM(a.customer)+SUM(a.clue) desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
			foreach ($list as $key => $item) {
				//线索数量
				$clue_map2['iid'] = array('eq', $iid);
				$clue_where2['uid'] = array('eq', $item['uid']);
				$clue_where2['distributor_id'] = array('eq', $item['uid']);
				$clue_where2['_logic'] = 'or';
				$clue_map2['_complex'] = $clue_where2;
				$clue_count = D('clue')->where($clue_map2)->count();
				$list[$key]['clue_count'] = $clue_count ? $clue_count : 0;

				$clue_total_count = $clue_total_count + $clue_count;

				//客户数量
				$customer_map2['iid'] = array('eq', $iid);
				$customer_map2['uid'] = array('eq', $item['uid']);
				$customer_count = D('customer')->where($customer_map2)->count();
				$list[$key]['customer_count'] = $customer_count ? $customer_count : 0;

				$customer_total_count = $customer_total_count + $customer_count;

				$gj_total_customer = $gj_total_customer + $item['customer'];

				$gj_total_clue = $gj_total_clue + $item['clue'];

				//线索与客户联系数总和
				$list[$key]['total_contact_number'] = $item['customer'] + $item['clue'];

				//线索与客户总和
				$list[$key]['total_count'] = $list[$key]['clue_count'] + $list[$key]['customer_count'];
			}

			$this->assign('customer_count', $customer_total_count);

			$this->assign('clue_count', $clue_total_count);

			$this->assign('gj_customer', $gj_total_customer);

			$this->assign('gj_clue', $gj_total_clue);

			$gj_total = $gj_total_customer + $gj_total_clue;
			$this->assign('gj_total', $gj_total);

			$total = $customer_total_count + $clue_total_count;
			$this->assign('total', $total);
		} else {
			$map['a.iid'] = $iid;
			$map['a.pa_id'] = array('gt', 0);
			$map['a.is_pay'] = array('eq', 1);
			$map['a.type'] = array(array('eq', 1), array('eq', 2), array('eq', 3), 'OR');

			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'] . " 00:00:00")), array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59")), 'AND');
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time'] . " 00:00:00"));
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time'] . " 23:59:59"));
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			if ($_REQUEST['name'] != '') {
				$map['b.username'] = array('like', '%' . $_REQUEST['name'] . '%');
				$this->assign('name', $_REQUEST['name']);
			}

			$count_arr = D('order a')->join('left join teach_ucenter_member b on a.pa_id = b.id')->where($map)->group('a.pa_id')->select();
			$count = count($count_arr);
			$Page = new \Think\Page($count, 10, array('rank_type' => $rank_type, 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time'], 'name' => $_REQUEST['name']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = D('order a')->field('a.pa_id as id,SUM(a.pay_price) as price,b.username')->join('left join teach_ucenter_member b on a.pa_id = b.id')->where($map)->group('a.pa_id')->order('SUM(a.pay_price) desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		}

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->assign('rank_type', $rank_type);

		$this->assign('cur', '排行榜');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 报名排行
	 */
	public function singUpRanking()
	{
		$iid = $_REQUEST['iid'];
		$map['a.iid'] = array('eq', $iid);
		$map['a.obj_id'] = array('gt', 0);

		if ($_REQUEST['username'] != '') {
			$map['b.username'] = array('like', '%' . $_REQUEST['username'] . '%');
			$this->assign('username', $_REQUEST['username']);
		}

		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'])), 'AND');
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		} else {
			if (!empty($_REQUEST['start_time'])) {
				$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
				$this->assign('start_time', $_REQUEST['start_time']);
			}
			if (!empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time']));
				$this->assign('end_time', $_REQUEST['end_time']);
			}
		}

		$count = M('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->count();
		$Page = new \Think\Page($count, 10, array('username' => $_REQUEST['username'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('signup_ranking a')->field('a.obj_id,SUM(a.face) as face,SUM(a.live) as live,SUM(a.video) as video,SUM(a.face)+SUM(a.live)+SUM(a.video) as total,b.username')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->order('SUM(a.face)+SUM(a.live)+SUM(a.video)')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		//直播课学员总数
		$live = D('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->SUM('live');
		//点播课学员总数
		$video = D('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->SUM('video');
		//面授课学员总数
		$face = D('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->SUM('face');

		$this->assign('live', $live);
		$this->assign('video', $video);
		$this->assign('face', $face);

		$this->meta_title = '报名排行';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 联系数排行
	 */
	public function contactNumberRanking()
	{
		$iid = $_REQUEST['iid'];
		$map['a.iid'] = array('eq', $iid);
		$map['a.obj_id'] = array('gt', 0);

		if ($_REQUEST['username'] != '') {
			$map['b.username'] = array('like', '%' . $_REQUEST['username'] . '%');
			$this->assign('username', $_REQUEST['username']);
		}

		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'])), 'AND');
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		} else {
			if (!empty($_REQUEST['start_time'])) {
				$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
				$this->assign('start_time', $_REQUEST['start_time']);
			}
			if (!empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time']));
				$this->assign('end_time', $_REQUEST['end_time']);
			}
		}

		$count = M('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->count();
		$Page = new \Think\Page($count, 10, array('username' => $_REQUEST['username'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('signup_ranking a')->field('a.obj_id,SUM(a.face) as face,SUM(a.live) as live,SUM(a.video) as video,SUM(a.face)+SUM(a.live)+SUM(a.video) as total,b.username')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->order('SUM(a.face)+SUM(a.live)+SUM(a.video)')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '联系数排行';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 营业额排行
	 */
	public function turnoverRanking()
	{
		$iid = $_REQUEST['iid'];
		$map['a.iid'] = array('eq', $iid);
		$map['a.obj_id'] = array('gt', 0);

		if ($_REQUEST['username'] != '') {
			$map['b.username'] = array('like', '%' . $_REQUEST['username'] . '%');
			$this->assign('username', $_REQUEST['username']);
		}

		if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
			$map['a.create_time'] = array(array('egt', strtotime($_REQUEST['start_time'])), array('elt', strtotime($_REQUEST['end_time'])), 'AND');
			$this->assign('start_time', $_REQUEST['start_time']);
			$this->assign('end_time', $_REQUEST['end_time']);
		} else {
			if (!empty($_REQUEST['start_time'])) {
				$map['a.create_time'] = array('egt', strtotime($_REQUEST['start_time']));
				$this->assign('start_time', $_REQUEST['start_time']);
			}
			if (!empty($_REQUEST['end_time'])) {
				$map['a.create_time'] = array('elt', strtotime($_REQUEST['end_time']));
				$this->assign('end_time', $_REQUEST['end_time']);
			}
		}

		$count = M('signup_ranking a')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->count();
		$Page = new \Think\Page($count, 10, array('username' => $_REQUEST['username'], 'start_time' => $_REQUEST['start_time'], 'end_time' => $_REQUEST['end_time']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = D('signup_ranking a')->field('a.obj_id,SUM(a.face) as face,SUM(a.live) as live,SUM(a.video) as video,SUM(a.face)+SUM(a.live)+SUM(a.video) as total,b.username')->join('left join teach_ucenter_member b on b.id = a.obj_id')->where($map)->group('a.obj_id')->order('SUM(a.face)+SUM(a.live)+SUM(a.video)')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '营业额排行';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 跟进记录
	 */
	public function follow_up_log()
	{
		$iid = session('iid');
		$map['iid'] = array('eq', $iid);
		$map['uid'] = array('gt', 0);
		$search = $_REQUEST['search'] ? $_REQUEST['search'] : 1;

		$count_list = D('follow_up_log a')->where($map)->group("uid")->select();
		$count = count($count_list);
		$Page = new \Think\Page($count, 5);
		$list = D('follow_up_log')->field('uid,SUM(customer) as customer,SUM(clue) as clue')->where($map)->group('uid')->order("SUM(customer)+SUM(clue) desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
		foreach ($list as $key => $item) {
			//线索数量
			$clue_map2['iid'] = array('eq', $iid);
			$clue_where2['uid'] = array('eq', $item['uid']);
			$clue_where2['distributor_id'] = array('eq', $item['uid']);
			$clue_where2['_logic'] = 'or';
			$clue_map2['_complex'] = $clue_where2;
			$clue_count = D('clue')->where($clue_map2)->count();
			$list[$key]['clue_count'] = $clue_count ? $clue_count : 0;

			//客户数量
			$customer_map2['iid'] = array('eq', $iid);
			$customer_map2['uid'] = array('eq', $item['uid']);
			$customer_count = D('customer')->where($customer_map2)->count();
			$list[$key]['customer_count'] = $customer_count ? $customer_count : 0;

			//线索与客户联系数总和
			$list[$key]['total_contact_number'] = $item['customer'] + $item['clue'];

			//线索与客户总和
			$list[$key]['total_count'] = $list[$key]['clue_count'] + $list[$key]['customer_count'];

			$username = D('ucenter_member')->where(array('id' => $item['uid']))->getField('username');
			$list[$key]['username'] = $username ? $username : '';
		}

		$customer_count = D('customer')->where(array('iid' => $iid))->count();
		$this->assign('customer_count', $customer_count);

		$clue_count = D('clue')->where(array('iid' => $iid))->count();
		$this->assign('clue_count', $clue_count);

		$gj_customer = D('follow_up_log')->where(array('iid' => $iid))->SUM('customer');
		$this->assign('gj_customer', $gj_customer);

		$gj_clue = D('follow_up_log')->where(array('iid' => $iid))->SUM('clue');
		$this->assign('gj_clue', $gj_clue);

		$gj_total = $gj_customer + $gj_clue;
		$this->assign('gj_total', $gj_total);

		$total = $customer_count + $clue_count;
		$this->assign('total', $total);

		$this->assign('_list', $list);

		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		if ($search == 1) {
			$line_time = date('m-d', time());
			$this->assign('line_time', $line_time);

			$start_time = strtotime(date('Y-m-d 00:00:00', time()));
			$end_time = strtotime(date('Y-m-d 23:59:59', time()));
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer from teach_follow_up_log where iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time}");
			$line_obj_data = $line_data_arr[0]['customer'];
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = $line_data_arr[0]['clue'];
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $line_obj_data);
			$this->assign('clue_data', $line_type_data);
		} else if ($search == 2) {
			$start_time = strtotime(date('Y-m-d 00:00:00', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)));
			$end_time = strtotime(date('Y-m-d 23:59:59', time()));
			$line_time = "";
			$line_obj_data = "";
			$line_type_data = "";

			$customer_data = 0;
			$clue_data = 0;
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up_log where iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by FROM_UNIXTIME(create_time,'%Y%m%d') asc");
			foreach ($line_data_arr as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$line_obj_data .= $item['customer'] . ",";
				$line_type_data .= $item['clue'] . ",";
				$customer_data = $customer_data + $item['customer'];
				$clue_data = $clue_data + $item['clue'];
			}

			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$line_obj_data = mb_substr($line_obj_data, 0, mb_strlen($line_obj_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = mb_substr($line_type_data, 0, mb_strlen($line_type_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $customer_data);
			$this->assign('clue_data', $clue_data);
		} else if ($search == 3) {
			$start_time = strtotime(date('Y-m-01 00:00:00', time()));
			$end_time = strtotime(date('Y-m-d 23:59:59', time()));

			$line_time = "";
			$line_obj_data = "";
			$line_type_data = "";

			$customer_data = 0;
			$clue_data = 0;
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up_log where iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by FROM_UNIXTIME(create_time,'%Y%m%d') asc");
			foreach ($line_data_arr as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$line_obj_data .= $item['customer'] . ",";
				$line_type_data .= $item['clue'] . ",";

				$customer_data = $customer_data + $item['customer'];
				$clue_data = $clue_data + $item['clue'];
			}

			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$line_obj_data = mb_substr($line_obj_data, 0, mb_strlen($line_obj_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = mb_substr($line_type_data, 0, mb_strlen($line_type_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $customer_data);
			$this->assign('clue_data', $clue_data);
		} else if ($search == 4) {
			$start_time = strtotime(date('Y-m-d 00:00:00', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3, 1, date('Y'))));
			$end_time = strtotime(date('Y-m-d 23:59:59', time()));
			$line_time = "";
			$line_obj_data = "";
			$line_type_data = "";

			$customer_data = 0;
			$clue_data = 0;
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up_log where iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by FROM_UNIXTIME(create_time,'%Y%m%d') asc");
			foreach ($line_data_arr as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$line_obj_data .= $item['customer'] . ",";
				$line_type_data .= $item['clue'] . ",";

				$customer_data = $customer_data + $item['customer'];
				$clue_data = $clue_data + $item['clue'];
			}

			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$line_obj_data = mb_substr($line_obj_data, 0, mb_strlen($line_obj_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = mb_substr($line_type_data, 0, mb_strlen($line_type_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $customer_data);
			$this->assign('clue_data', $clue_data);
		} else if ($search == 5) {
			$start_time = strtotime(date('Y-01-01 00:00:00', time()));
			$end_time = strtotime(date('Y-m-d 23:59:59', time()));
			$line_time = "";
			$line_obj_data = "";
			$line_type_data = "";

			$customer_data = 0;
			$clue_data = 0;
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up_log where iid = {$iid} and create_time >= {$start_time} and create_time <= {$end_time} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by FROM_UNIXTIME(create_time,'%Y%m%d') asc");
			foreach ($line_data_arr as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$line_obj_data .= $item['customer'] . ",";
				$line_type_data .= $item['clue'] . ",";

				$customer_data = $customer_data + $item['customer'];
				$clue_data = $clue_data + $item['clue'];
			}

			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$line_obj_data = mb_substr($line_obj_data, 0, mb_strlen($line_obj_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = mb_substr($line_type_data, 0, mb_strlen($line_type_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $customer_data);
			$this->assign('clue_data', $clue_data);
		} else {
			$where = "iid = {$iid}";
			$start_time = strtotime($_REQUEST['start_time']);
			$end_time = strtotime($_REQUEST['end_time'] . " 23:59:59");
			if (!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])) {
				$where .= " and create_time >= {$start_time} and create_time <= {$end_time}";
				$this->assign('start_time', $_REQUEST['start_time']);
				$this->assign('end_time', $_REQUEST['end_time']);
			} else {
				if (!empty($_REQUEST['start_time'])) {
					$where .= " and create_time >= {$start_time}";
					$this->assign('start_time', $_REQUEST['start_time']);
				}
				if (!empty($_REQUEST['end_time'])) {
					$where .= " and create_time <= {$end_time}";
					$this->assign('end_time', $_REQUEST['end_time']);
				}
			}

			$person_id = $_REQUEST['person_id'];
			if (!empty($person_id)) {
				$where .= " and uid = " . $person_id;
				$this->assign('person_id', $person_id);
			}

			$line_time = "";
			$line_obj_data = "";
			$line_type_data = "";

			$customer_data = 0;
			$clue_data = 0;
			$line_data_arr = D()->query("select SUM(clue) as clue,SUM(customer) as customer,FROM_UNIXTIME(create_time,'%Y%-%m%-%d') as create_time from teach_follow_up_log where {$where} group by FROM_UNIXTIME(create_time,'%Y%m%d') order by FROM_UNIXTIME(create_time,'%Y%m%d') asc");
			foreach ($line_data_arr as $key => $item) {
				$line_time .= $item['create_time'] . ",";
				$line_obj_data .= $item['customer'] . ",";
				$line_type_data .= $item['clue'] . ",";

				$customer_data = $customer_data + $item['customer'];
				$clue_data = $clue_data + $item['clue'];
			}

			$line_time = mb_substr($line_time, 0, mb_strlen($line_time, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_time', $line_time);

			$line_obj_data = mb_substr($line_obj_data, 0, mb_strlen($line_obj_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_obj_data', $line_obj_data);

			$line_type_data = mb_substr($line_type_data, 0, mb_strlen($line_type_data, 'UTF-8') - 1, 'UTF-8');
			$this->assign('line_type_data', $line_type_data);

			//饼图数据
			$this->assign('customer_data', $customer_data);
			$this->assign('clue_data', $clue_data);
		}

		$this->assign('search', $search);

		//负责人
		$person_map['user_type'] = array(array('eq', 3), array('eq', 4), 'OR');
		$person = D('ucenter_member')->where($person_map)->select();
		$this->assign('person', $person);

		$this->assign('cur', '跟进记录');
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 添加代购
	 */
	public function addPurchasingAgent()
	{
		if (IS_POST) {
			$data['iid'] = $_REQUEST['iid'];
			$data['pa_id'] = session('user_id');
			$id_card = $_REQUEST['id_card'];//身份证号
			$id_account = $_REQUEST['id_account'];//id账号

			$course_id = $_REQUEST['course_id'];
			$course_type = $_REQUEST['course_type'];//课程类型（1直播，2面授，3在线）
			$course_price = $_REQUEST['course_price'];

			$order_sn = "";
			if ($course_type == 1) {
				$order_sn = 'ZB';
				$course = D('live_course')->where(array('id' => $course_id))->find();
			} else if ($course_type == 2) {
				$order_sn = 'MS';
				$course = D('face_course')->where(array('id' => $course_id))->find();
			} else {
				$order_sn = 'ZX';
				$course = D('video_course')->where(array('id' => $course_id))->find();
			}

			$order_sn = $order_sn . date('Ymd', time()) . rand(1000, 9999);

			if ($id_card == '') {
				$this->error("请输入身份证号");
			}

			if (empty($course_id)) {
				$this->error("请选择课程");
			}

			$member = D('ucenter_member')->where(array('id_card' => $id_card))->find();
			$data['uid'] = $member['id'];

			$data['order_sn'] = $order_sn;
			$data['pay_order_sn'] = $order_sn;
			$data['pay_price'] = $course_price;
			$data['type'] = $course_type;

			$time = time();
			$data['update_time'] = $time;
			$data['create_time'] = $time;
			$order_id = D('order')->add($data);
			if ($order_id) {
				$vice_order_data['order_id'] = $order_id;
				$vice_order_data['to_id'] = $course_id;
				$vice_order_data['img'] = $course['img'];
				$vice_order_data['name'] = $course['name'];
				if ($course_type == 2) {
					$vice_order_data['live_open_time'] = $course['live_time'];
				} else if ($course_type == 3) {
					//$vice_order_data['expire_time'] = $course['live_time'];
				}
				$vice_order_data['old_price'] = $course_price;
				$vice_order_data['price'] = $course_price;
				$vice_order_data['update_time'] = $time;
				$vice_order_data['create_time'] = $time;
				D('vice_order')->add($vice_order_data);

				if ($id_account != '') {
					D('ucenter_member')->where(array('id_card' => $id_card))->save(array('id_account' => $id_account));
				}
			} else {
				$this->error("添加代购失败");
			}
		} else {
			$this->display();
		}
	}

	/**
	 * Mr.Jiang
	 * 代购记录
	 */
	public function purchasingAgentList()
	{
		$iid = $_REQUEST['iid'];
		$pa_id = session('user_id');
		$map['iid'] = array('eq', $iid);
		$map['pa_id'] = array('eq', $pa_id);
		$count = M('order')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('order')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '代购记录';
		$this->display();
	}

	/**
	 * Mr.Jiang
	 * 删除代购
	 */
	public function delPurchasingAgent()
	{
		$id = $_REQUEST['id'];
		$count = M('order')->where(array('id' => $id))->count();
		if ($count <= 0) {
			$this->error("代购记录不存在或已被删除");
		}

		$result = D('order')->where(array('id' => $id))->delete();
		if ($result) {
			$this->success("删除成功");
		} else {
			$this->error("删除失败");
		}
	}

	/**
	 * Mr.Jiang
	 * 首页
	 */
	public function home()
	{
		$iid = $_REQUEST['iid'];
		$uid = session('user_id');
		//线索
		$clue_map['iid'] = array('eq', $iid);
		$clue_where['uid'] = array('eq', $uid);
		$clue_where['distributor_id'] = array('eq', $uid);
		$clue_where['_logic'] = 'or';
		$clue_map['_complex'] = $clue_where;
		$clue_count = D('clue')->where($clue_map)->count();

		//已转
		$clue_where['source'] = array('eq', 6);
		$transferred = D('clue')->where($clue_map)->count();

		//转客户率
		$customerTransferRate = intval($clue_count / $transferred);

		//客户总数
		$customer_map['iid'] = array('eq', $iid);
		$customer_map['uid'] = array('eq', $uid);
		$customer_count = D('customer')->where($customer_map)->count();

		//成交客户
		$deal_customer_map['iid'] = array('eq', $iid);
		$deal_customer_map['uid'] = array('eq', $uid);
		$deal_customer_map['type'] = array('eq', 1);
		$deal_customer_count = D('follow_up')->where($deal_customer_map)->group('cid')->count();

		//成交率
		$dealRate = intval($customer_count / $deal_customer_count);

		//介绍客户
		$introduce_map['iid'] = array('eq', $iid);
		$introduce_map['uid'] = array('eq', $uid);
		$introduce_map['source'] = array('eq', 5);
		$introduce_customer_count = D('customer')->where($introduce_map)->count();

		//介绍率
		$introduceRate = intval($deal_customer_count / $introduce_customer_count);

		//营业额
		$order_map['iid'] = array('eq', $iid);
		$order_map['pa_id'] = array('eq', $uid);
		$order_map['is_pay'] = array('eq', 1);
		$turnover = D('order')->where($order_map)->sum('pay_price');
		$turnover = sprintf("%.1f", $turnover / 10000);

		//今日营业额
		$day_order_map['iid'] = array('eq', $iid);
		$day_order_map['pa_id'] = array('eq', $uid);
		$day_order_map['is_pay'] = array('eq', 1);
		$day_order_map['pay_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', time()))), array('elt', strtotime(date('Y-m-d 23:59:59', time()))), 'AND');
		$dayTurnover = D('order')->where($day_order_map)->sum('pay_price');
		$dayTurnover = sprintf("%.1f", $dayTurnover / 10000);

		//跟进
		$follow_map['iid'] = array('eq', $iid);
		$follow_map['uid'] = array('eq', $uid);
		$follow_count = D('follow_up')->where($follow_map)->count();

		//今日跟进
		$day_follow_map['iid'] = array('eq', $iid);
		$day_follow_map['uid'] = array('eq', $uid);
		$day_follow_map['create_time'] = array(array('egt', strtotime(date('Y-m-d 00:00:00', time()))), array('elt', strtotime(date('Y-m-d 23:59:59', time()))), 'AND');
		$day_follow_count = D('follow_up')->where($day_follow_map)->count();
	}

	/**
	 * 退出登录
	 * */
	public function outLogin()
	{
		session('user_id', null);
		session('user_name', null);
		session('iid', null);
		$this->success('退出登录成功', U('Home/Login/login'));
	}
}
